C#以独占模式打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/685135/
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
open file in exclusive mode in C#
提问by George2
I want to open a file for read in exclusive mode, and if the file is already opened by some process/thread else, I want to receive an exception. I tried the following code, but not working, even if I opened the foo.txt, I still can reach the Console.WriteLine statement. Any ideas?
我想以独占模式打开一个文件进行读取,如果该文件已经被其他进程/线程打开,我想收到一个异常。我尝试了以下代码,但不起作用,即使我打开了 foo.txt,我仍然可以访问 Console.WriteLine 语句。有任何想法吗?
static void Main(string[] args)
{
using (Stream iStream = File.Open("c:\software\code.txt", FileMode.Open,
FileAccess.Read, FileShare.None))
{
Console.WriteLine ("I am here");
}
return;
}
回答by Cerebrus
I would suggest using the FileAccess.ReadWrite
member because some files may already be open but allow you Read
access on the file. However, I would guess that in non-exceptional conditions, all files open for Read/Write
access would not allow your code to Write
to the file.
我建议使用该FileAccess.ReadWrite
成员,因为某些文件可能已经打开但允许您Read
访问该文件。但是,我猜想在非异常情况下,所有打开供Read/Write
访问的文件都不允许您的代码Write
访问该文件。
Of course (as Mehrdad already explained), if you are using an editor such as Notepad to open the file as a test, you will not be able to restrict access because Notepad does not lock the file at all.
当然(正如 Mehrdad 已经解释过的),如果您使用记事本等编辑器打开文件作为测试,您将无法限制访问,因为记事本根本不会锁定文件。
回答by Mehrdad Afshari
What you are doing is the right thing. Probably you are just testing it incorrectly. You should open it with a program that locks the file when it's open. Notepad wouldn't do. You can run your application twice to see:
你正在做的是正确的事情。可能你只是在错误地测试它。您应该使用在打开时锁定文件的程序打开它。记事本不行。您可以运行您的应用程序两次以查看:
static void Main(string[] args)
{
// Make sure test.txt exists before running. Run this app twice to see.
File.Open("test.txt", FileMode.Open, FileAccess.Read, FileShare.None);
Console.ReadKey();
}
回答by Jakob Christensen
FileShare.None will only work if another process has also opened the file without allowing it to be shared for reads.
FileShare.None 仅在另一个进程也打开了该文件而不允许共享读取时才起作用。
Programs such as Notepad and Visual Studio do not lock text files.
记事本和 Visual Studio 等程序不会锁定文本文件。
回答by lakshmanaraj
What you have done is correct.
你所做的是正确的。
If you need what are all files already opened, then there is a way to see by NtQuerySystemInformation
如果你需要已经打开的所有文件,那么有一种方法可以通过 NtQuerySystemInformation 查看
You may get idea from http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx
您可能会从http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx获得想法
which gets all the files opened in a directory.. which can be extended to a single file whether opened or not...
它获取在目录中打开的所有文件......无论是否打开,都可以扩展为单个文件......
回答by Jim Mischel
Test it by writing a simple console mode program that opens the file and then waits:
通过编写一个简单的控制台模式程序来测试它,该程序打开文件然后等待:
static void Main(string args[])
{
using (FileStream f = File.Open("c:\software\code.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
Console.Write("File is open. Press Enter when done.");
Console.ReadLine();
}
}
Run that program from the command line (or another instance of Visual Studio), and then run your program. That way, you can play with different values for FileMode and FileShare to make sure that your program reacts correctly in all cases.
从命令行(或 Visual Studio 的另一个实例)运行该程序,然后运行您的程序。这样,您可以对 FileMode 和 FileShare 使用不同的值,以确保您的程序在所有情况下都能正确响应。
And, no, you don't have to check to see if the file is open first. Your code should throw an exception if the file is already open. So all you have to do is handle that exception.
而且,不,您不必先检查文件是否已打开。如果文件已经打开,您的代码应该抛出异常。所以你所要做的就是处理那个异常。