Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Can I Delete Open Image Files in .NET using FileInfo?

Tags:

c#

.net

file-io

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

like image 264
Minh Nguyen Avatar asked Jan 23 '23 13:01

Minh Nguyen


1 Answers

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.

like image 117
Fredrik Mörk Avatar answered Feb 03 '23 20:02

Fredrik Mörk