简体   繁体   English

FileShare ReadWrite无法正常工作(C#.NET)

[英]FileShare ReadWrite not working (C#.NET)

I'm using a hex-editor control for C#, the source code and binary files can be found here. 我正在使用C#的十六进制编辑器控件,可以在这里找到源代码和二进制文件

One problem when using it was that if a file was loaded in the hex-editor and another program, the other program can't save the file, because it's already being used by another process. 使用它时的一个问题是,如果在十六进制编辑器和另一个程序中加载了文件,则另一个程序无法保存该文件,因为它已被另一个进程使用。

So I asked the author of the control who told me to set the FileShare argument in the File.Open method in the FileByteProvider and DynamicFileByteProvider class to ReadWrite (it was originally only Read) would fix it. 所以我问控件的作者谁告诉我在FileByteProvider和DynamicFileByteProvider类的File.Open方法中将FileShare参数设置为ReadWrite(它最初只是Read)会修复它。 So I did that, but it still didn't work (same error). 所以我这样做了,但它仍然无效(同样的错误)。 Setting it to Write only also didn't work, but setting it to Read only and None both work. 将其设置为“仅写入”也不起作用,但将其设置为“只读”和“无”都有效。 The files have the same problem in any program, for example Notepad. 这些文件在任何程序中都有相同的问题,例如记事本。 They aren't set to ReadOnly or anything, so I have no idea why it doesn't work. 它们没有设置为ReadOnly或任何东西,所以我不知道为什么它不起作用。

Is there anything I am missing here? 这里有什么我想念的吗?

The problem might be with the other program - if it's trying to open the file for exclusive access (with no sharing), it doesn't matter how your program has opened the file - it's going to fail. 该问题可能是与其他程序-如果它试图打开进行独占访问文件(不共享),它并没有多么你的程序打开文件-它是要失败的。


Whenever a program tries to open a file, you specify a FileAccess and FileShare parameter (or defaults are taken if they're not explicitly passed). 每当程序尝试打开文件时,都指定FileAccess和FileShare参数(如果未显式传递,则采用默认值)。

What Windows then has to do is check all existing open file handles, and determine whether they're compatible. Windows然后要做的是检查所有现有的打开文件句柄,并确定它们是否兼容。 So it compares your FileAccess parameter against everyone else's FileShare parameters - are you allowed to do what everyone else has said they're happy for others to do? 因此,它将您的FileAccess参数与其他人的FileShare参数进行比较 - 您是否可以做其他人所说的他们为其他人做的事情而做的事情? And then it performs the opposite check - does your FileShare parameter match up with their FileAccess parameters? 然后它执行相反的检查 - 您的FileShare参数是否与其FileAccess参数匹配? - are they doing things that you're happy for them to be doing? - 他们做的事情让你很高兴他们做的事吗? Only if both checks pass can your particular open request be approved. 只有两项检查都通过才能批准您的特定开放申请。

You can use something like Process Monitor to actually watch the Win32 calls being issued to CreateFile to see what each process is actually doing. 你可以使用Process Monitor之类的东西来实际观察发给CreateFile的Win32调用,看看每个进程实际上在做什么。


Notepad can open a file that's been shared for Read/Write, but it can't write back to the file. 记事本可以打开已为读/写共享的文件,但无法写回文件。 Sample program: 示例程序:

using System.IO;

namespace ConsoleApplication2
{
    class Program
    {

        static void Main(string[] args)
        {
            var fs = new FileStream(@"C:\Bar.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            fs.Write(System.Text.Encoding.ASCII.GetBytes("abc"),0,3);
            fs.Flush();
            fs.Close(); //<-- Breakpoint here
        }
    }
}

Set the given breakpoint, run the program. 设置给定的断点,运行程序。 When it hits the breakpoint, open Notepad, and use it to open C:\\Bar.txt. 当它到达断点时,打开记事本,并使用它打开C:\\ Bar.txt。 Everything is fine. 一切顺利。 Add more text to the file and hit save. 在文件中添加更多文本并点击保存。 You'll get an error message. 你会收到一条错误信息。

  1. Change to ReadWrite. 更改为ReadWrite。
  2. Recompile. 重新编译。
  3. Try opening a file that you haven't tried opening yet and check if the problem appears. 尝试打开尚未尝试打开的文件,并检查问题是否出现。

Possibly, you weren't closing the file appropriately so it remained open even after you closed your application (with previous permissions set to Read) 可能是您没有正确关闭文件,因此即使在您关闭应用程序之后它仍保持打开状态(以前的权限设置为Read)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM