📅  最后修改于: 2023-12-03 15:10:55.350000             🧑  作者: Mango
在C#中,我们可以使用Directory.Exists()
方法来检查文件夹是否存在。
以下是Directory.Exists()
方法的语法:
public static bool Exists(string path);
参数:
path
:要检查的文件夹的路径。返回值:
true
。false
。以下示例演示了如何使用Directory.Exists()
方法检查文件夹是否存在:
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\Users\UserName\Desktop\TestFolder";
if (Directory.Exists(folderPath))
{
Console.WriteLine("文件夹存在!");
}
else
{
Console.WriteLine("文件夹不存在!");
}
Console.ReadLine();
}
}
如果文件夹存在,则输出为文件夹存在,否则输出为文件夹不存在。
path
应为有效的路径。System.IO
命名空间。Directory.Exists()
方法返回true
,则不一定说明文件夹是可供访问的。如果没有适当的访问权限,则可能无法访问文件夹。以上就是在C#中检查目录是否存在的方法,希望对您有所帮助!