File.Open 读取访问被拒绝在 Windows 中执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7007671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File.Open for read access denied on executing file in Windows
提问by Vegar Westerlund
I have a problem with file permissions on executing files in Windows that seems to be solved after following a forum tip[1], but I cannot understand why. Maybe you guys can help.
我在 Windows 中执行文件时遇到文件权限问题,在遵循论坛提示 [1] 后似乎已解决,但我不明白为什么。也许你们可以帮忙。
I'm checking the banner of a file by executing it (reading the console output) and then opening the same file for reading afterwards using FileStream:
我正在通过执行文件(读取控制台输出)来检查文件的横幅,然后使用 FileStream 打开相同的文件以供读取:
public void fileMD5(string filename) {
if (!File.Exists(filename)) return NT.Fail("File does not exist: " + filename);
BinaryReader stream = new BinaryReader(File.Open(filename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
int bufferSize = 4096;
byte[] buffer = new byte[bufferSize];
int readBytes;
while ((readBytes = stream.Read(buffer, 0, bufferSize)) > 0) {
md5Hasher.TransformBlock(buffer, 0, readBytes, buffer, 0);
}
stream.Close();
}
fileMD5('sample.exe');
and every once in a while I would get "file is being used by another process". From Wikipedia I know that Windows will set a lock on executing files denying write access[2], but I'm only reading. Also the process should have stopped already when I try to open it.
每隔一段时间我就会得到“文件正在被另一个进程使用”。从维基百科我知道 Windows 会锁定执行文件拒绝写访问 [2],但我只是在阅读。此外,当我尝试打开它时,该过程应该已经停止。
From the forum post it would seem that adding a FileShare.ReadWrite would help and it seems that It does:
从论坛帖子看来,添加 FileShare.ReadWrite 会有所帮助,而且似乎确实如此:
FileStream stream = File.Open('sample.exe',
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
But I don't understand why. Is there a race condition here that I don't see?
但我不明白为什么。这里有我看不到的竞争条件吗?
Also the File.Open call seems to be much quicker with FileShare.ReadWrite instead of the default (which I guess is FileShare.Read).
此外,使用 FileShare.ReadWrite 而不是默认(我猜是 FileShare.Read)的 File.Open 调用似乎要快得多。
[1] http://www.xtremevbtalk.com/archive/index.php/t-192118.html
[1] http://www.xtremevbtalk.com/archive/index.php/t-192118.html
[2] http://en.wikipedia.org/wiki/File_locking#In_Microsoft_Windows
[2] http://en.wikipedia.org/wiki/File_locking#In_Microsoft_Windows
回答by lowds
When you do not specify a FileShare parameter the default for this option is FileShare.None, in fact the code within the File class simply executes this:
当您不指定 FileShare 参数时,此选项的默认值是 FileShare.None,实际上 File 类中的代码只是执行以下操作:
public static FileStream Open(string path, FileMode mode, FileAccess access)
{
return File.Open(path, mode, access, FileShare.None);
}
With regards to the performance I can only imagine that specifying FileShare.ReadWrite means that Windows does not need to aquire a lock on the file.
关于性能,我只能想象指定 FileShare.ReadWrite 意味着 Windows 不需要获取文件锁定。
As far as the "file is being used by another process" error you are getting does this issue go away if you wrap the usage of the stream variable within a using block so that the Stream gets disposed of as soon as you are done?
至于“文件正在被另一个进程使用”错误,如果您将流变量的使用包装在 using 块中,以便在您完成后立即处理 Stream,则此问题会消失吗?
using (var stream = File.Open('sample.exe', FileMode.Open, FileAccess.Read))
{
//do something with the stream here
}
回答by Skomski
You should close your FileStream and then afterwards open a new FileStream.
您应该关闭 FileStream,然后再打开一个新的 FileStream。
FileShare is needed when applications want to share a file not only one application or an application has multiple readers or writers at the same time.
当应用程序不仅要共享一个文件,而且要共享文件时,需要 FileShare,而不仅仅是一个应用程序或一个应用程序同时具有多个读取器或写入器。
Why? It's get messy when everyone can read and write at the same time. In this case you should better explicitly set it so its clear that its get messy. :)
为什么?当每个人都可以同时阅读和写作时,它会变得一团糟。在这种情况下,您应该更好地明确设置它,以便清楚它变得混乱。:)
回答by Lorenzo Dematté
It has to do with the underlying windows API CreateFile flags. See http://msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspxfor an overview, http://blogs.msdn.com/b/larryosterman/archive/2004/05/13/131263.aspxfor en explanation on how NT (and following) load the execs with FILE_SHARE_DELETE
它与底层的 Windows API CreateFile 标志有关。见http://msdn.microsoft.com/en-us/library/aa363858%28v=vs.85%29.aspx的概述, http://blogs.msdn.com/b/larryosterman/archive/2004/ 05/13/131263.aspx用于解释 NT(及以下)如何使用 FILE_SHARE_DELETE 加载 execs
And especially this
尤其是这个
http://blogs.msdn.com/b/oldnewthing/archive/2004/05/11/129759.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2004/05/11/129759.aspx
For a great explanation on how the share permission and desired access compose together!
有关共享权限和所需访问权限如何组合在一起的一个很好的解释!
回答by user1182735
It seems like specifying the wrong FileShare can prohibit you from accessing a file. If you specify FileShare.Read
but some other application has currently write access to that file, you cannot access the file, since your FileShare.Read is currently not satisfiable. FileShare.ReadWrite is less restrictive, because its easier satisfiable. Source: http://blogs.msdn.com/b/oldnewthing/archive/2004/05/11/129759.aspx
似乎指定错误的 FileShare 会阻止您访问文件。如果您指定FileShare.Read
但某些其他应用程序当前对该文件具有写访问权限,则您无法访问该文件,因为您的 FileShare.Read 当前无法满足。FileShare.ReadWrite 限制较少,因为它更容易满足。来源:http: //blogs.msdn.com/b/oldnewthing/archive/2004/05/11/129759.aspx