2

While I was reading about the method System.IO.File.Exists(string path), I was puzzled by the sentence

The Exists method should not be used for path validation, this method merely checks if the file specified in path exists.

To me the two statements "File in this path exists" and "the path [to the file] is valid" are almost synonyms, so I'm missing something. What? Why shouldn't I validate a path with File.Exists()?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
  • 5
    Because the file at the end of the path might not exist, but your path can. Or, it might be better said that "if it looks like a duck, quacks like a duck, and smells like a duck, then it should validate the thing that it says it's validating". – C Bauer Oct 01 '14 at 14:55
  • My first thought was that it is cautioning you that `File.Exists` will not confirm whether a given path is valid - meaning something like `1ekj2#34\/3423&*^%*` is not a valid path OR (hence) a file. However the wording of that passage makes me think something else might be up... – George Mauer Oct 01 '14 at 14:58
  • @CBauer Well, obviously File.Exists() is concerned with paths to files, not to directories, which would be Directory.Exists(). You suggest the sentence means "don't use it to validate the path to the containing directory"? – Peter - Reinstate Monica Oct 01 '14 at 14:58
  • This isn't a c# thing by the way, it's the .Net BCL since this would be the same from any .Net language – George Mauer Oct 01 '14 at 14:59
  • @PeterSchneider I just meant that if you write code that says "CheckDirectory()" and it checks for a specific file in that directory, then the name of the method is misleading and may fail if that specific file isn't present, even if it normally would be present. – C Bauer Oct 01 '14 at 15:07

4 Answers4

7

Because of the next sentence:

Passing an invalid path to Exists returns false

Which normally always generates an exception. Just not in the case of File.Exists():

   bool miracle = File.Exists(":::");

So be careful, the file might actually exist but you simply might have fumbled the string. You can't tell, there is no exception that warns you about this.

Do keep in mind that is not where the trouble with File.Exists() ends. You cannot trust a true return value either. A hazard of running code on a multi-tasking operating system, the file might be deleted a nanosecond later by another process. That can cause some very mystifying crashes of course :) Best to avoid it completely, just try to open the file (that's atomic) and catch the exception if it isn't there.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • And passing a valid path? Still false. Perhaps this is another meaning to that first sentence? – Steve Oct 01 '14 at 15:01
  • I see. They mean syntactical "validation". Hm. That could be done with FileInfo (http://stackoverflow.com/questions/422090/in-c-sharp-check-that-filename-is-possibly-valid-not-that-it-exists).-- And thanks for the hint with open(), worth keeping in mind. – Peter - Reinstate Monica Oct 01 '14 at 15:13
5

What if the path is syntactically incorrect? Exists() would return false implying that you now could create now that file, which is not true.

plinth
  • 48,267
  • 11
  • 78
  • 120
  • Well, only people who haven't read the documentation would do that ;-). – Peter - Reinstate Monica Oct 01 '14 at 15:25
  • Yes, I think this short answer gets to the center: MSDN's "validation" was meant syntactically, as becomes clear (now) when I re-read the rest of their paragraph about how to test for invalid chars. I guess my idea of "but I *am* validating very successfully!" was too ingrained. – Peter - Reinstate Monica Oct 01 '14 at 15:33
0

Because these are two different things... As C Bauer pointed out the path can exists but the file may not.

If the path does not exist this will throw one exception but if the file at the end of the path does not exist this will return false which you can use in an if statement. An example of how to use this is:

if(System.IO.File.Exists(path))
{
    //write to this file
}
else
{
    //create the file then write to it...
}

I use this for log files in software all the time so I can have one function to do all the writing to the file and the first time the function is called it will just create the file and then write to it. Every other time it will just add lines to the file.

Edit: spelling

DotN3TDev
  • 138
  • 9
0

I think that this means...

If the file exists in that path.. FOUND

If the path doesn't exists in that path... NOT FOUND

But if the file not exists and you give a NOT OK... this doesn't mean that the path doesn't exists... it means that the FILE doesn't exists..

You can have the path, but not the file, and get as return a NOT FOUND

Oscar Bralo
  • 1,912
  • 13
  • 12