File Locked by Services (after service code reading the text file)
Posted
by rvpals
on Stack Overflow
See other posts from Stack Overflow
or by rvpals
Published on 2010-06-08T21:15:47Z
Indexed on
2010/06/08
21:32 UTC
Read the original article
Hit count: 288
I have a windows services written in C# .NET. The service is running on a internal timer, every time the interval hits, it will go and try to read this log file into a String.
My issue is every time the log file is read, the service seem to lock the log file. The lock on that log file will continue until I stop the windows service. At the same time the service is checking the log file, the same log file needs to be continuously updated by another program. If the file lock is on, the other program could not update the log file.
Here is the code I use to read the text log file.
private string ReadtextFile(string filename)
{
string res = "";
try
{
System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.StreamReader sr = new System.IO.StreamReader(fs);
res = sr.ReadToEnd();
sr.Close();
fs.Close();
}
catch (System.Exception ex)
{
HandleEx(ex);
}
return res;
}
Thank you.
© Stack Overflow or respective owner