-2

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

Screenshot

Screenshot of the error

I also tried the equivalent function in Python:

os.path.isfile(r"D:\Design\SVG\black_circle.svg")
//output -- True

And this screenshot shows the file system.

Screenshot of FileSystem

What makes File.Exists() return false?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oansari
  • 325
  • 2
  • 7
  • 1
    I don't believe you. Prove that file exists really – Renatas M. May 22 '15 at 13:58
  • 3
    So you're saying that the new screenshot represents the actual situation, and that both your C# code and your Python code are looking for an SVG file, and C# says it's not there and Python says it is? And you've verified that the file is actually there on disk? – adv12 May 22 '15 at 14:04
  • @user3380608: what do you want from us, how could we help you? We don't know if the file really exists and you haven't shown any code which we could have a look at. – Tim Schmelter May 22 '15 at 14:05
  • @TimSchmelter I want to know why `File.Exists()` returning false while the file is there – oansari May 22 '15 at 14:12
  • 5
    Could be a permission problem. [This](http://stackoverflow.com/questions/18266637/why-system-io-file-existsstring-path-returns-false) will help you. – Gopichandar May 22 '15 at 14:18
  • Is the file on a server, or another computer? Is the file on the same hard drive as the program? – Drew Kennedy May 22 '15 at 14:19
  • @DrewKennedy it on in same computer and the same hard drive – oansari May 22 '15 at 14:21
  • @Gopichandar Thanks, but the permission is fine for the file, even though if it is permission problem why python code was able to find the file while c# code couldn't? – oansari May 22 '15 at 14:25
  • FWIW, [this is the guts](http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,56cd161c65ab07fe) of what's going on, showing it needing to be able to _read_ the file as per @Gopichandar's comment, rather than it just needing to check if it is there. Try following Jon Skeet's suggestion in the linked question and perform a read on it to get further error information. – James Thorpe May 22 '15 at 14:26
  • 2
    Could it be: The file is on a drive mapped to `D:\` for the current user, but you are running Visual Studio as an Administrator, therefore the drive is not mapped when debugging? – Matthew Watson May 22 '15 at 14:26
  • Have you doubled backslashes \\ in 'path'? –  May 22 '15 at 14:29
  • @JamesThorpe I got **NotSupportedException**, _the givin path's format is not supported_ – oansari May 22 '15 at 14:31
  • What _exactly_ is `path` set to? – James Thorpe May 22 '15 at 14:33
  • @user3380608: maybe it would help if you showed also the path the you have used in C#. – Tim Schmelter May 22 '15 at 14:33
  • The path in c# code `string path = @"‪D:\Design\SVG\black_circle.svg";` – oansari May 22 '15 at 14:37
  • I can't reproduce what it is you're seeing, and if your permissions are indeed fine, then I have no idea what exactly is going on. Something in the background is preventing your program from accessing the file. Try executing `if(Directory.Exists(@"D:\Design\SVG\")) {...}` and see what the result is. – Drew Kennedy May 22 '15 at 14:48
  • I can certainly reproduce the thing I mentioned above: Can see a file when running from Visual Studio WITHOUT elevated permissions, but can't see the same file with the same code when running from Visual Studio WITH elevated permissions. – Matthew Watson May 22 '15 at 15:06
  • @MatthewWatson I solved this issue, it is not related to permissions, if the path copied from the explorer to visual studio, then I won't be able to read the file, but if I wrote it in visual studio then it will work – oansari May 22 '15 at 15:15

2 Answers2

2

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
Community
  • 1
  • 1
oansari
  • 325
  • 2
  • 7
  • 1
    This doesn't make any sense. Why would `copiedPath` ever have a question mark in it, and why would that throw off the number of times `':'` appears? You said you did `string path = @"‪D:\Design\SVG\black_circle.svg";` then `if (!File.Exists(path)) { throw new FileNotFoundException("File not found"); }` Something doesn't sound right, here. – vapcguy Oct 17 '17 at 19:43
0

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; 
   }
Pepik
  • 111
  • 1
  • 6