I’d like to open the same file for both reading and writing. The file pointer should be independent. So a read operation should not move the write position and vice versa.
Currently, I’m using this code:
FileStream fileWrite = File.Open (path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
FileStream fileRead = File.Open (path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
StreamWriter _Writer = new StreamWriter (fileWrite, new ASCIIEncoding ());
StreamReader _Reader = new StreamReader (fileRead, new ASCIIEncoding ());
But that leads to an IOException: “The process cannot access the file because it is being used by another process”
I think I just figured it out myself. In the second
File.Open, we’re trying to deny other applications write access by specifyingFileShare.Read. Instead, we need to allow the first stream to write to the file:That’s inherently correct, as the reading stream should not care about other people writing to the file. At least I don’t get an exception anymore.