//但它生成的是指定路径下所有文件或文件夹的FileSystemInfo
//不用判断string是文件还是文件夹,就可以判断它是否存在想法不妥,
if(File.Exists(string) || Directory.Exists(string)){
//do sth
}
//不用判断string是文件还是文件夹,就可以判断它是否存在想法不妥,
if(File.Exists(string) || Directory.Exists(string)){
//do sth
}
using System.IO;string vFileName = @"c:\temp";
if (File.Exists(vFileName) || Directory.Exists(vFileName))
{
MessageBox.Show("路径“" + vFileName + "”存在");
}
我就是觉得这样太麻烦了,才要用FileSystemInfo的
如果获得了它的实例fsi,直接fsi.Exists()就行了抽象类不能通过构造函数生成实例,但并不代表不能用其他方法,比如DirectoryInfo.GetFileSystemInfos就可以
只不过它生成的是指定路径下所有文件或文件夹的FileSystemInfo,不符合这里的要求我想知道有其他方法么?
public static FileSystemInfo GetFileSystemInfo(string path)
{
// 这里判断
if (File.Exists(path))
return new FileInfo(path);
else if (Directory.Exists(path))
return new DirectoryInfo(path);
else
throw new SomeException();}