【发布时间】:2014-05-26 11:12:29
【问题描述】:
除非尝试访问当前日期的日志文件,否则我的 C# 应用程序运行良好,因为它正被另一个进程使用,但该文件可以通过记事本打开。几周前我问了这个问题,但没有收到解决我问题的答案。
private static void WriteFile(string fileToRead, string fileToWrite, string mySearchString, xmlData counter)
{
counter.hitcount = 0;
Stream stream = File.Open(fileToRead, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
{
StreamReader sr = new StreamReader(stream);
while (!sr.EndOfStream)
{
using (var sw = new StreamWriter(fileToWrite, true))
{
if (sw.BaseStream.Position == 0)
{
//write header only once
sw.WriteLine("date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Cookie) sc-status sc-substatus sc-win32-status sc-bytes time-taken");
}
var count = 1;
while (sr.Peek() != -1)
{
var line = sr.ReadLine();
// skips 4 lines (headers of log file)
if (count > 4)
{
if (line != null && line.Contains(mySearchString))
{
sw.WriteLine(line);
counter.hitcount++;
}
}
count++;
sr.Close();
sw.Close();
}
}
}
}
}
目标:我想阅读今天的日志文件,但它目前正在使用中。阅读文件后,我将某些字符串提取到一个新的文本文件中。但是我要读取的文件正在被另一个进程使用
【问题讨论】:
-
你为什么又问同样的问题?
-
在临时位置复制日志文件,然后进行处理。锁定问题的可能性较小。轻松重试。
-
能够在记事本中打开它只是意味着您可以读取文件,并不一定也可以写入文件。跨度>
-
再问同样的问题会有所帮助...怎么样?
-
如果其他进程正在使用日志文件,则无法写入日志文件(读锁 =/= 写锁)。
标签: c#