我正在编写一个解析Windows服务的日志文件的程序。该程序运行良好,它可以读取和解析正确的日志文件等。问题是,当我试图在服务处于活动状态时运行它时,它会返回一个IOError,我猜是因为文件被“锁定”了或类似的东西。有解决办法吗?这是我用来读取和解析文件的代码:
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;
});
}发布于 2011-04-25 08:05:02
替换
new StreamReader(s)
使用
new StreamReader(File.OpenRead(s))或
new StreamReader(new FileStream(s, FileMode.Open))
https://stackoverflow.com/questions/5499260
复制相似问题