I have a friend who is in disagreement with me on this, and I'm just looking to get some feedback as to who is right and wrong in this situation.
FileInfo file = ...;
if (file.Exists)
{
//File somehow gets deleted
//Attempt to do stuff with file...
}
The problem my friend points out is that, "so what if the file exists when I check for existence? There is nothing to guard against the chance that right after the check the file gets deleted and attempts to access it result in an exception. So, is it even worth it to check for existence before-hand?"
The only thing I could come up with is that MSDN clearly does a check in their examples, so there must be more to it. MSDN - FileInfo. But, it does have me wondering... is the extra call even worth it?
file.Exists
bit redundant. On the other hand, if I were processing a large number of files, many of which might be absent, I might stick thefile.Exists
bit in there to avoid the overhead of triggering the exception-handling mechanism. – Oh