If you’re using the System.IO.File.Exists method to determine whether a file is present in a particular location, then the answer to the question posed in the title of this post might be: when you don’t have permissions to see it. This might seem obvious, but actually it caught me a little bit off guard this morning. In general I’m conditioned to think that a permissions failure will throw an exception. After all, one way to interpret the rule might be: you don’t have permissions to access the file, therefore there is no way to answer your question. Another way is: you don’t have permissions to access the file, therefore as far as you’re concerned it doesn’t exist. Here’s the MSDN lowdown on the Exists method:
If the caller does not have sufficient permissions to read the specified file, no exception is thrown and the method returns false regardless of the existence of path.
Obviously Microsoft’s library designers agreed with the second interpretation. Not sure I like it, but at least now I know that the file might be there even when File.Exists says it isn’t.