Boolean Boolean - 12 days ago 9
C# Question

C# Problem with opened files


Possible Duplicate:

Is there a way to check if a file is in use?




Is there a way to close a file in C#?

if(File.Exists(TEMP_FILENAME))
File.Delete(TEMP_FILENAME);


The above code complains that file is already used by another process.

Is there a way to check the open file handles in VS 2008?

Answer

Close any streams using the file in your application.

If the error is due to another running applicaton, it's best to just try the operation, and deal with the failure. If you check, then try to perform the operation, you create a race condition. If the delete fails (whether due to file in use or permissions), just deal with the problem then.