开发者

File in use detection not working for pictures/folders

开发者 https://www.devze.com 2023-03-11 11:26 出处:网络
I am using this code to show an error when file is open: try { stream开发者_StackOverflow社区 = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消