With # Directory.CreateDirectory (path), should I check if the path exists first?

I need to copy some files to a directory, but sometimes this directory does not exist yet and must be created first. In most cases, the directory will exist, although it must be created only once.

I know that I can just put Directory.CreateDirectory() before the copy of the file, 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 already exists, so it’s faster to check if it exists first and not to call CreateDirectory() in any case.

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

I don't care how long it takes to create a directory, given that users will only do this once. I'm more interested in whether the if works with Directory.Exists() faster than the time it takes for Directory.CreateDirectory() to find out if the directory already exists.

"It seems to me that it is" wasteful "to call Directory.CreateDirectory() every time, although it probably exists, so I first" feel better ", but it still calls the function anyway, so I really get advantage of extinction - does my code with additional if and Directory.Exists() operators check?

+5
source share
3 answers

You can check the Directory code in the Microsoft Reference source. The first step in Directory.CreateDirectory is to call Directory.Exists (or the internal equivalent, InternalCreateDirectory ). Therefore, using two methods, you kind of write this:

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

Personally, I will never do an Exists check.

+7
source

There is no reason to check existence first. Directory.CreateDirectory already handling this case for you. From the documentation :

Creates all directories and subdirectories in the specified path, if they do not already exist.

The work done internally will be approximately the same - but if it does not exist, you double-check that it slows down, because Directory.Exists must do all the internal normalization. For more information, see the reference source .

+2
source

You definitely need to call Directory.Exists() first to check if this folder exists and then call Directory.CreateDirectory() only if it is not. This is not code ugliness, this is good practice.

And as for which works faster, you can use the System.Diagnostics.Stopwatch class to compare the time spent on the CreateDirectory() version and the if (Directory.Exists(...)) version if (Directory.Exists(...)) . The difference will be very small if the directory already exists, and you should not care about it, since you will not get anything in performance by optimizing such things.

0
source

Source: https://habr.com/ru/post/1209578/


All Articles