How to use FileStream to add to a file without an exclusive lock?

What I'm trying to do with FileStreamin C # /. NET, is to open two streams: one application for the file, and the other is reading asynchronously (for unit testing the network connection processing code). I can’t figure out how to make the write stream open the file without exception, and therefore the code always throws an exception:

The process cannot access the file 'C: \ test.txt' because it is being used by another process.

Here is some code that demonstrates the problem:

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);
+3
source share
2 answers

See this question: C # file read / write file not working

, freader FileShare.Write, , .

+5

, , , ?

0

Source: https://habr.com/ru/post/1698122/


All Articles