简体   繁体   English

在使用C#创建目录之前,是否需要检查目录是否存在?

[英]Is there any point in checking if a directory exists before creating it in C#?

When I want to do anything that involves reading and writing to directories I have always been in the habit of checking to see if a directory exists and if it doesn't I will create it. 当我想做涉及读和写目录的任何事情时,我总是习惯检查目录是否存在,如果不存在,我将创建它。 Is there any point in doing this? 这样做有什么意义吗? If i run the following code i get no exception. 如果我运行以下代码,我不会例外。 Creating a directory that already exists does nothing bad. 创建一个已经存在的目录并没有什么不好。

Directory.CreateDirectory("C:\\test");
Directory.CreateDirectory("C:\\test");

To me checking first seems like a waste of resources. 对我来说,先检查似乎是浪费资源。 If the directory doesnt exist, then 2 commands need to be executed. 如果该目录不存在,则需要执行2条命令。 I would imagine that CreateDirectory performs a check before doing anything, so it would most definitely be redundant to do this if this is the case. 我可以想象CreateDirectory在执行任何操作之前先执行检查,因此,在这种情况下,这样做绝对是多余的。

So to pitch the question. 所以提出这个问题。 When creating directories, should i do the following or can i ditch the if statement? 创建目录时,应该执行以下操作还是可以放弃if语句?

if(Directory.Exists("C:\\test")) Directory.CreateDirectory("C:\\test");

Note - I am aware that the performance benefit here is incredibly tiny, I am more interested in keeping code tidy and ditching lines i dont need. 注意-我知道这里的性能优势非常之小,我对保持代码整洁和不需要的行更感兴趣。

It is not created if it allready exists according to msdn 根据msdn,如果它已经存在,则不会创建

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. 除非在路径中指定的目录已经存在或路径的某些部分无效,否则将创建该目录中指定的所有目录。 If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory. 如果目录已经存在,则此方法不会创建新目录,但会为现有目录返回DirectoryInfo对象。

You can also check the source here and see that a check if the directory exists is performed. 您也可以在此处检查源并检查目录是否存在。

Directory.CreateDirectory : Directory.CreateDirectory

Creates all directories and subdirectories in the specified path unless they already exist. 除非已存在,否则在指定路径中创建所有目录和子目录。

If your intent is to either create new or use existing directory, then you don't need to check for existence. 如果您打算创建新目录或使用现有目录,则无需检查是否存在。

Moreover, reading about Directory.Exists : 此外,阅读有关Directory.Exists

The Exists method returns false if any error occurs while trying to determine if the specified file exists 如果尝试确定指定文件是否存在时发生任何错误,则Exists方法将返回false

Exists can't be used to know for sure if CreateDirectory will sucess, so you are right, it doesn't make sense. Exists不能用来确定CreateDirectory是否会成功,所以您是对的,这没有任何意义。 Any exception (eg invalid name) will still be thrown during CreateDirectory . CreateDirectory期间仍将引发任何异常(例如,无效名称)。

It only make sense if you have to do something when directory is existing already, eg close opened file located in this directory or add this info into log. 仅当目录已存在时您必须执行某些操作(例如,关闭位于此目录中的打开文件或将此信息添加到日志中)才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM