在微软提供的介绍中,File和FileInfo有完整的介绍。
我目前只整理,自己用到的方法。比如判断文件是否存在。
static void Main(string[] args)
{
string path = @"E:\testfile01.txt";
bool flag = File.Exists(path);
if (flag)
{
Console.WriteLine("文件存在");
}
else
{
Console.WriteLine("文件不存在");
}
Console.Read();
}
在微软的文档网页上,介绍的很详细。还说Exist方法不应使用的路径验证时,若要检查目录是否存在,使用Directory.Exists。File.Exists只是检查在指定的文件是否存在。
下面是用FileInfo类来,完成检查指定文件是否存在。
static void Main(string[] args)
{
string path = @"E:\testfile01.txt";
FileInfo fInfo = new FileInfo(path);
bool flag = fInfo.Exists;
if (flag)
{
Console.WriteLine("文件存在");
}
else
{
Console.WriteLine("文件不存在");
}
Console.Read();
}
这里是微软的介绍,FileInfo是需要实例一个有string类型的构造函数,而且Exists是对象的一个属性。
那么,我此时有这样的问题。为何有File静态类和FileInfo实例类,两种呢,它们都有具备操作文件的功能。
现在,不纠结这个问题。使用.net framework提供的两个类,来检查文件是否存在,我会了。
来源:https://www.cnblogs.com/158-186/p/10939532.html