Non Blocking streamreader?


Dec 11, 2014
Programming Experience
I am reading a log file from another system to report errors more quickly with the following:

while ((sLine = reader.ReadLine()) != null)
//check result of each line and do something

Problem is that the system is failing log writes because i have the file locked. One option is to copy the file to another location and then read it. Is there a more elegant solution though? I see StreamReader.Peek() but I am not sure if that is the right solution? I am not trying to see if I can read, rather, I need to leave the file available for the other system to write to at any time. Maybe copy is the best. Hmm...
By default StreamReader opens a FileStream with FileShare.Read sharing mode. You can open a FileStream yourself with FileShare.ReadWrite sharing mode, and pass that to a new StreamReader. This will allow the other system to subsequently open the file for writing when you are currently reading it.
Top Bottom