Do I need to check if the directory / file exists or not?

Which code is better?

Code1:

if (!Directory.Exists("DirectoryPathHere")) Directory.CreateDirectory("DirectoryPathHere"); 

Codex2:

 Directory.CreateDirectory("DirectoryPathHere"); 

I think that Code2 , because since I saw that it does not give any errors and does not create a new folder when the folder already exists, therefore, although checking for the existence of the folder is useless. Right?

+4
source share
5 answers

You do not need to check if the directory exists, the method does this for you. If you checked MSDN :

All and all directories specified in the path are created if they already exist or if some part of the path is invalid. Path The parameter specifies the path to the directory, not the path to the file. If a directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for an existing directory.

+5
source

I would use the DirectoryInfo class to check if it exists, and maybe even if it exists, check the directory permissions if my current runtime permissions are insufficient to access the contents or update the directory. You must apply exception handling to any method you go with; what if, for example, there is a file with a directory name?

+2
source

The main thing is that the CreateDirectory method implicitly checks if the directory exists before trying to create it.

To read the code, it is better to first use the explicit Directory.Exists method.

I also strongly agree with @SimonWhitehead on the defensive programming front. Showing that you know that the pit is falling ... and actively actively opposing them explicitly in your code is good :)

 I think we can all see the fact that the second method does the same, but, is it cheaper in terms of being more readable? No. 

Anyone who knows the framework will probably not agree, and I can too. But:

Always indicate as if the person who is finishing work with your code is a violent psychopath who knows where you live.

http://www.codinghorror.com/blog/2008/06/coding-for-violent-psychopaths.html

Edit 2: I have a strange feeling that the compiler is doing this. Assembly programmers could detect this before the production of the IL.

+1
source

Here is a simple code from http://msdn.microsoft.com/en-us/library/54a0at6s.aspx

 using System; using System.IO; class Test { public static void Main() { // Specify the directory you want to manipulate. string path = @"c:\MyDir"; try { // Determine whether the directory exists. if (Directory.Exists(path)) { Console.WriteLine("That path exists already."); return; } // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(path); Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path)); // Delete the directory. di.Delete(); Console.WriteLine("The directory was deleted successfully."); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally {} } } 
0
source

You do not need to check it, but since there are a lot of problems when processing files and folders, it is better than the try-catch to handle any possible problems:

 try { Directory.CreateDirectory("DirectoryPathHere"); } catch (Exception ex) { MessageBox.Show("Error: "+ex.Message); } 

you can also add finally if necessary.

0
source

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


All Articles