views:

292

answers:

3

Hi,

We have some C# code that reads data from a text file using a StreamReader. On one computer we can read data from the text file even after it has been deleted or replaced with a different text file - the File.Exists call reports that the file exists even when it doesn't in Windows Explorer. However, on another computer this behaviour doesn't happen. Both computers are running Vista Business and .NET 2.0.50727 SP2.

We have tried restarting the machine without a resolution.

Does anyone have any understanding on how this could be possible and information about possible solutions?

Thanks, Alan

+2  A: 

Could this be a folder virtualization issue?

Daniel Brückner
That would be my first guess.
ur
Thanks for your help, folder virtualization was causing the conflict.
Alan Spark
Vista is really funny, I was not aware of Folder Virtualization at all, but after reading the article at link, it made me laugh that programmer can spend a whole life trying to understand what went wrong !!
Akash Kava
+4  A: 

From MSDN

The Exists method should not be used for path validation, this method merely checks if the file specified in path exists.
Be aware that another process can potentially do something with the file in between the time you call the Exists method and perform another operation on the file, such as Delete. A recommended programming practice is to wrap the Exists method, and the operations you take on the file, in a try...catch block as shown in the example. This helps to narrow the scope for potential conflicts. The Exists method can only help to ensure that the file will be available, it cannot guarantee it.

SwDevMan81
A: 

Is the file being opened for reading before it's being deleted? If it is, it's not unexpected to still be able to read from the opened file even after the filesystem has otherwise let it go.

RE: File.Exists():

File.Exists is inherently prone to race-conditions. It should not be used as the exclusive manner to verify that a file does or doesn't exist before performing some operation. This mistake can frequently result in a security flaw within your software.

Rather, always handle the exceptions that can be thrown from your actual file operations that open, etc, and verify your input once it's open.

Greg D