File in use detection not working for pictures/folders
I am using this code to show an error when file
is open:
try
{
stream开发者_StackOverflow社区 = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
MessageBox.Show("file is open!");
}
The problem is that code is not working with pictures (jpg, bmp and other) and with folders.
Is there anything that I am missing?
Don't know if I understand what you want to do... I guess you want to find out whether someone is viewing that image currently.
A file is "open" only if some application has an open handle to it. When you use a picture viewer, it is possible (and quite likely), that the viewer opens the file, reads it, and closes it immediately. Hence, the file itself is not use.
This applies to all sorts of files, but many are kept open on purpose (e.g. Office applications will typically keep their files open).
In general, you shouldn't assume that the "file handle is open" and "a human user in some way 'uses' a file" actions are related at all. The file could be open, but no human is interacting (maybe a search deamon is indexing it). On the other hand, a file could be "visible" to the user, but closed on the file system.
this code will not work in your scenario. Then you open the picture system reads the file, uncompress the image and show it to you and closes the file. You can delete it while it is open in Windows.
精彩评论