Non Blocking streamreader?

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
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...
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,194
Location
Norway
Programming Experience
10+
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