简体   繁体   中英

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):

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? .

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.

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; 
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM