【发布时间】:2014-10-22 00:44:02
【问题描述】:
我正在尝试编写一个函数来确定文件是否存在。这两种方法被证明返回不一致的结果(与返回误报的 isFileFound() 相比,fileExists() 似乎提供了准确的结果 - 我在尝试创建实例时会出现异常)。
protected bool isFileFound(string path, string fileName)
{
System.IO.FileInfo fi = null;
bool found = false;
try
{
fi = new System.IO.FileInfo(path + fileName);
found = true;
}
catch (Exception e)
{
baselogger.Fatal(e.Message + " " + e.StackTrace + " \n" + path + fileName);
}
return found;
}
protected bool fileExists(string path, string pattern)
{
bool success = false;
try
{
success = File.Exists(path + pattern);
}
catch (Exception e)
{
baselogger.Warn(e.Message + " " + e.StackTrace + " " + e.Source);
}
return success;
}
似乎都无法解析以下语法的 UNC 路径:\\abcserver\c$\xyzfolder\foo.bar
如果知道这些方法的 unc 路径为何失败,我们将不胜感激。
【问题讨论】:
-
仅供参考 - 作为一般做法,我尝试使用 System.IO.Path.Combine(path, filename) 而不是字符串连接:路径 + 文件名
-
警告:IO.Path.Combine("D:\Files", "\foo.xml") 会给你"\foo.xml",接下来你知道你有文件保存到根目录。
标签: c# filesystems