FileShare ReadWrite无法正常运行(C#.NET) [英] FileShare ReadWrite not working (C#.NET)

查看:352
本文介绍了FileShare ReadWrite无法正常运行(C#.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为C#使用十六进制编辑器控件,可以在此处找到源代码和二进制文件./a>

I'm using a hex-editor control for C#, the source code and binary files can be found here.

使用它的一个问题是,如果文件已加载到十六进制编辑器和另一个程序中,则另一个程序将无法保存该文件,因为该文件已被另一个进程使用.

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.

因此,我问控件的作者谁告诉我将FileByteProvider和DynamicFileByteProvider类的File.Open方法中的FileShare参数设置为ReadWrite(最初仅是Read)可以解决该问题.所以我做到了,但是还是没用(相同的错误).将其设置为只写"也不起作用,但是将其设置为只读"和无"都起作用.这些文件在任何程序中都存在相同的问题,例如记事本.它们没有设置为ReadOnly或任何东西,所以我不知道为什么它不起作用.

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. 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.

这里有什么我想念的吗?

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.

每当程序尝试打开文件时,都要指定FileAccess和FileShare参数(如果未明确传递默认值,则采用默认值).

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).

然后Windows要做的是检查所有现有的打开文件句柄,并确定它们是否兼容.因此,它将您的FileAccess参数与其他人的FileShare参数进行比较-您是否允许他人执行其他人说的对其他人高兴的事情?然后执行相反的检查-您的FileShare参数与他们的FileAccess参数匹配? -他们所做的事情让您感到高兴吗?仅当两项检查均通过时,您的特定公开请求才能获得批准.

What Windows then has to do is check all existing open file handles, and determine whether they're compatible. 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? And then it performs the opposite check - does your FileShare parameter match up with their FileAccess parameters? - 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.

您可以使用类似 Process Monitor 之类的方法来实际观察Win32调用颁发给 CreateFile 来查看每个进程的内容确实在做.

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.

记事本可以打开已为读取/写入共享的文件,但无法写回该文件.示例程序:

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
        }
    }
}

设置给定的断点,运行程序.当它碰到断点时,打开记事本,并使用它打开C:\ Bar.txt.一切都好.在文件中添加更多文本,然后点击保存.您会收到一条错误消息.

Set the given breakpoint, run the program. When it hits the breakpoint, open Notepad, and use it to open C:\Bar.txt. Everything is fine. Add more text to the file and hit save. You'll get an error message.

这篇关于FileShare ReadWrite无法正常运行(C#.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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