[英]File.Exists() returns false in C#
This is weird.这很奇怪。 The file is already exists, but I am still getting false when using File.Exist(path):该文件已经存在,但是在使用 File.Exist(path) 时我仍然会出错:
string path = @"D:\Design\SVG\black_circle.svg";
I found the answer in an answer to Stack Overflow question What is causing NotSupportedException (“The given path's format is not supported”) while using a valid path? 我在对堆栈溢出问题的答案中找到了答案,是什么在使用有效路径时导致NotSupportedException(“不支持给定路径的格式”)? . 。
If I used the path which is directly copied from Windows Explorer, C# wouldn't be able to read the file, and the IndexOf(':')
will be 2, but if wrote the path in Visual Studio it would work fine and IndexOf(':')
will be 1. 如果使用从Windows资源管理器直接复制的路径,C#将无法读取该文件,并且IndexOf(':')
将为2,但是如果在Visual Studio中写入该路径,它将可以正常工作,并且IndexOf(':')
将为1。
string copiedPath = @"?D:\Design\SVG\black_circle.svg";
int a = copiedPath.IndexOf(':') //output 2
string hardCodedpath = @"D:\Design\SVG\black_circle.svg";
int i = hardCodedpath .IndexOf(':'); //output 1
This code correctly detects the file existence.此代码正确检测文件存在。
FileInfo f = new FileInfo(@"C:\Program Files\Microsoft Office\root\Office16\MSACCESS.EXE");
if (f.Exists)
{
... do something;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.