判断文件或者文件夹是否存在
首先,文件File,文件夹Directory,需要引用System.IO
using System.IO;
定义一个需要判断的文件的地址
string filePath = "C:\\test.txt" ;
然后利用File进行判断
if (File.Exists(filePath))
{
Console.WriteLine("文件{0}存在.", filePath);
}
else
{
Console.WriteLine("文件{0}不存在.", filePath);
}
运行结果如下图所示
定义一个文件夹的地址
string folderPath ="C:\\Windows";
利用Directory进行判定
string folderPath = "C:\\Windows";
if (Directory .Exists (folderPath ))
{
Console.WriteLine(" 文件夹{0}存在.", folderPath);
}
else
{
Console.WriteLine("文件夹{0}不存在.", folderPath);
}
Console.ReadKey();
运行结果如下图