As per solution posted in following post I have created form to read log files
BackgroundWorker & Timer, reading only new lines of a log file?
However I am getting file in use
exception when writing log entries.
help
As per solution posted in following post I have created form to read log files
BackgroundWorker & Timer, reading only new lines of a log file?
However I am getting file in use
exception when writing log entries.
help
You need to make sure that your log reading code opens the file in ReadWrite mode. See the excellent answer to this other post for a full explanation: How do I open an already opened file with a .net StreamReader?
When I do such file access/manipulation I usually take care of two things.
First, for reading I use the following code (see FileShare enumeration):
using (Stream s = File.Open(path, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)) { ... }
Second, I usually write a while loop for opening the file for reading/writing like this (draft code):
int tries=0;
while (tries < 10) {
try {
// try to open file for your operation
break;
} catch (IOException) {
tries++;
Thread.Sleep(200);
}
}
EDIT: Accidently I used FileShare.Read first time in my answer instead of the more appropriate FileShare.ReadWrite. Now I've corrected it.