views:

188

answers:

5

Possible Duplicate:
show a message if the filename already exists

Reading some answers with code samples I notice that those where this method mentioned are subjected to criticism. I'm using this method in my code. So I'd like to know if someone give me detailed response whuy this method is not recomemnded and what alternative approaches are?

+2  A: 

I dont think I have seen any references to File.Exists not working, but have had experience where a basic File.Exists is not enough. For instance if you are checking a file exists to perform further processing or copying etc you might want an extra check to see if the file can be opened exclusively. The file might exist but it also might be opened by another application or still be in the process of being copied / uploaded / generated.

Mark Redman
+7  A: 

I think the problem lies that if you check if a file exists using File.Exists and it passes the check; the issue is that another process could delete the file in the mean time.

  1. You check the file exists
  2. Carry on processing
  3. Another process deletes the file
  4. You try to open the file (without checking if it exists or not because you already checked right?

Another solution which I actually posted here is to the open file so that another process cannot delete or modify it.

  1. Open the file
  2. Carry on processing
  3. Another process CANNOT access file
  4. Save your changes and close file
Barry
+17  A: 

I guess that you are referring to this pattern:

// A
if (File.Exists(myFilename))
{
    // B
    // Do something which assumes that myFilename definitely exists
}

The problem here is that on a platform which has pre-emptive multi-tasking (such as Windows), there is nothing to stop some other process deleting myFilename between the places I have marked A and B.

As a result, file access code should always be defensive - that is, be prepared to handle FileNotFoundException, UnauthorizedAccessException, and indeed all the other exceptions that can occur during IO operations (see eg the docs for File.ReadAllText).

This is not to deny the usefulness of using File.Exists as an additional earlier check - if, say, the user has entered a filename manually, a sanity check that it exists will catch typos - but this can never be the last check.

AakashM
You were faster! +1
Michael Stoll
+1  A: 

Think about the information you get from File.Exist(). It tells you, that the file has existet, when you called the method, but it can be deleted in the meantime.

Sample:

if (File.Exists("myfile.txt"))
    File.Delete("myfile.txt");

This seams quite simple. But it is possible that the current thread is interruppted right after evaluating the if clause. Then another thread/process can delete the file. And so the file can be non existent, when File.Delete() is called.

Michael Stoll
A: 

I am troubled by the statement that we shouldn't use File.Exists() and its ilk. I understand the argument, but really, what the hell else are we supposed to do if we want to access a file resource?

OK, another process could delete the file in the time between checking it exists and accessing its content, but since we have absolutely no way to guarantee success we should code defensively, accepting the situation may occur, and take whatever steps are required to ensure that if the file access fails we can recover from it.

ZombieSheep