Write text file without Read lock in C#
Author: Rizwan Ansari | Categories: Programming No Comments
Suppose there is a situation in which a service is writing a log file and there is another application which needs to open this file for reading. By default a file which is written is exclusively open by writer. To overcome this and make Write and Read non-exclusive use the following code.
var writer = new FileStream(logfileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
var reader = new FileStream(logfileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);