Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Directory.CreateDirectory( path ), Should I check if path exists first?

Tags:

c#

file

directory

I need to copy some files into a directory, but sometimes that directory doesn't already exist and has to be created first. Most of the time the directory will exist though, as it only has to be created once.

I know that I can just put Directory.CreateDirectory() before the file copy and CreateDirectory() will just return if the directory already exists.

string destFolder; // path to destination
string sourceFolder; // path to source
Directory.CreateDirectory( destFolder ); // this will work even if destFolder exists
File.Copy( sourceFolder + sourceFileName, destFolder + sourceFileName );

But I know that in almost every case the destination folder will already exist, so is it faster to check if it exists first, rather than calling CreateDirectory() anyway.

if( !Directory.Exists( destFolder ) ) // check if it exists first
{
    Directory.CreateDirectory( destFolder );
}
// now move on to using the folder

I don't really care how long it takes to create the directory considering that users will only ever do it once. I'm more interested in whether or not the if statement with Directory.Exists() is any faster than the time it takes for Directory.CreateDirectory() to figure out that the directory already exists.

It "feels" wasteful to me to call Directory.CreateDirectory() every time even though it probably exists, so I "feel better" checking first, but it's still a function call either way so am I really even getting an advantage by uglying-up my code with extra if statements and Directory.Exists() checks?

like image 234
Sheldon Avatar asked Dec 19 '14 23:12

Sheldon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

You can check the code of the Directory class out in the Microsoft Reference source. The first thing Directory.CreateDirectory does is call Directory.Exists (or the internal equivalent, InternalExists). So using the two methods you are kind of writing this:

if (!Directory.Exists(dir))
{
    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }
}

Personally I would never do the Exists check.

like image 191
Martin Brown Avatar answered Sep 21 '22 16:09

Martin Brown