我试图用 C#/.NET 中的 FileStream
做的是打开两个流:一个附加到文件,另一个异步读取这些写入(用于单元测试一些网络连接处理代码)。我不知道如何让 writer 流以非独占锁定模式打开文件,因此代码总是抛出异常:
The process cannot access the file 'C:\test.txt' because it is being used by another process.
下面是一些演示问题的代码:
FileStream fwriter = new FileStream("C:\\test.txt", FileMode.Append,
FileAccess.Write, FileShare.Read);
FileStream freader = new FileStream("C:\\test.txt", FileMode.Open,
FileAccess.Read, FileShare.Read);
最佳答案
看到这个问题:C# file read/write fileshare doesn’t appear to work
简而言之,您的读取器必须指定 FileShare.Write 以允许文件上已有编写器这一事实。
关于c# - 如何在没有独占锁的情况下使用 FileStream 附加到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/189239/