tags:

views:

212

answers:

2

Hi,

I have a small utility that deletes files from folders using .NET's FileInfo.Delete() method. Basically, I can catch an IOException if the file is currently open. This works for all files except for image files (JPG, GIF, BMP, ect.). When these types of files are open, the program is still able to delete them without throwing any exception. Have you encountered this before? How would go about detecting that these types of files are open so that I can prevent the deletion.

Thanks, Minh

+8  A: 

Whether or not you can delete a file while it is open depends more on the program in which the file is open than the file type. If the application for instance simply reads the file contents and then closes the file (as Notepad for instance), you will be able to delete the file while it is "open" (meaning that it is showing in Notepad). Excel on the other hand keeps the file open in a manner that you usually are not able even to copy it.

Fredrik Mörk
+1  A: 

Use a tool like WhoLockMe or Unlocker to see whether the file is really "open". As others have said this has nothing to do with the type of file, merely how the program reading it opens the file.

RichardOD