I m writing a program that parses the log files of a Windows service. The program works just fine, it can read and parse correctly log files and so on. The problem is, when I try to run it with the service active, it returns a IOError, I guess because the files are "locked" or something like that. Is there a workaround? This is the code I use to read and parse the files:
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
var t = Task<int>.Factory.StartNew(() =>
{
string[] arr = FileManager.getFiles(textBox1.Text);
string destination = textBox1.Text + "\backup\";
string filename = "backup.bak";
if (!Directory.Exists(@destination))
{
Directory.CreateDirectory(@destination);
}
StreamWriter w = File.AppendText(@destination+backupfile);
foreach (string s in arr)
{
string sLine = "";
StreamReader objReader = new StreamReader(s);
while (sLine != null)
{
sLine = objReader.ReadLine();
if (!String.IsNullOrEmpty(sLine))
{
List<string> output = Parser.parse(sLine);
StreamWriter hh = File.AppendText(@textBox2.Text + filename);
for(int i = 3; i < output.Count; i++)
{
string str = output[i];
if (!String.IsNullOrEmpty(str))
{
hh.WriteLine(str);
}
}
hh.Close();
w.WriteLine(sLine);
}
}
objReader.Close();
//File.Delete(s);
}
w.Close();
return 1;
});
}