文件使用中检测不适用于图片/文件夹? [英] File in use detection not working for pictures/folders?

查看:85
本文介绍了文件使用中检测不适用于图片/文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码在打开文件时显示错误:

I am using this code to show an error when file is open:

try
{
using (FileStream fileStream = new FileStream(@"C:\....", FileMode.Open, FileAccess.Read))
{

}
}
catch (IOException)
{
MessageBox.Show("file is open!");
}


问题是,该代码不适用于图片(jpg,bmp等)和文件夹


The problem is, that code is not working with pictures (jpg, bmp and other) and with folders
Is there anything that I am missing?

推荐答案

这取决于打开文件的方式.

文件夹永远不会打开"或正在使用",因此打开流不会失败是因为这个原因:尽管我不确定如果将文件夹读/写为流会得到什么...

其他文件,尤其是图像:大多数程序的行为都很好-它们打开文件,读取内容,然后再次关闭文件,将其释放给其他应用程序.那些不会经常宣誓就职的人,因为它们会破坏自动备份系统,如果使用该文件,该备份系统将无法复制该文件. MS Outlook就是这样一个令人讨厌的程序.
如果考虑到这一点,则可以打开-关闭-关闭图片文件.由于它们大部分都是压缩的,因此没有必要保持打开状态,因为无论如何您都无法重写文件的一小部分-您必须编写整个文件,否则压缩将无法正常工作!
It depends on what opens the file.

Folders are never "Open" or "in use", so your stream open would not fail for that reason: though I''m not sure what you would get if you read / wrote a folder as a stream...

Other files, particularly images: Most programs are well behaved - they open the files, read the contents, and close the file again, releasing it for other applications. Ones which don''t tend to get sworn at a lot, since they mess up automated backup systems, which can''t copy the file if it is in use. MS Outlook is one such annoying program.
If you think about it, it makes sense to open-read-close picture files. Since they are mostly compressed there is no point in keeping it open, since you cannot re-write a small part of the file anyway - you have to write the whole thing or the compression is not going to work!


您显示的此方法适用于在没有共享访问权限的情况下打开的所有文件.

可能会错过的是:为什么您认为某些代码会使图像文件保持打开状态.这不是大多数代码的工作方式.通常,程序会打开图像文件,读取数据,关闭文件并将数据用于任何目的.文件大部分时间保持可访问状态.

同样,可以打开文件进行非独占访问,因此可以通过其他一些代码打开同一文件.我认为在大多数情况下并非如此.

—SA
This method you show does work on all files opened without shared access.

What you may miss is this: why do you think some code will keep image files open. This is not how most of the code works. Usually programs open image files, read the data, close the files and use the data for whatever purpose. The files remain accessible most of the time.

Also, it is possible to open a file for non-exclusive access, so the same file can be opened by some other code. I don''t think this is the case in most situations.

—SA


我有什么办法可以做到吗
我的意思是打开图像时显示错误????
is there any way i can do this
i mean show error when image is open????


这篇关于文件使用中检测不适用于图片/文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆