如何从C#检查计算机桌面中是否存在文件夹



我需要从C#C:\Users\MyComputer\Desktop\Test检查计算机桌面上是否存在文件夹。如果不存在,我需要添加一个文件夹Test

我有创建文件夹Test 的代码

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
System.IO.Directory.CreateDirectory(desktopPath + "\Test");

但在此之前,我需要检查该文件夹是否存在。如何从C#进行检查?帮我完成这项任务。

提前谢谢。Srihari

我想你在找

目录。存在

if (!Directory.Exists(Path.Combine(desktopPath, "Test"))
{
     //directory doesn't exist
}

使用此进行检查

System.IO.Directory.Exists(desktopPath + "\Test");

你可以这样做,

if(!Directory.Exists(desktopPath + "\Test")
{
   System.IO.Directory.CreateDirectory(desktopPath + "\Test");
}

参考

您可以使用System.IO中的Directory.Exists()方法http://msdn.microsoft.com/en-us/library/system.io.directory.exists(v=vs.110).aspx

它相当简单:

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
bool exists = Directory.Exists(Path.Combine(desktopPath, "FolderName"))
if (!exists)
{
  //FOLDER DOESN'T EXIST...
}

最新更新