Write text file without Read lock in C#

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.

Writer

var writer = new FileStream(logfileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);

Reader

var reader = new FileStream(logfileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Leave a Reply

Your email address will not be published.