我正在使用 aFileSystemWatcher
来监视日志文件的更改。
日志文件由第 3 方应用程序写入。
一旦触发更改,我将尝试使用以下方式读取文件:
using (FileStream fs = new FileStream(e.FullPath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
StreamReader sr = new StreamReader(fs);
string s = sr.ReadLine();
}
有时它在线上失败using
。
其他时候它失败了sr.ReadLine()
,我认为这是因为第 3 方系统当前正在访问该文件。
我认为通过设置FileAccess.Read
我应该能够始终读取文件?
我如何确保这不会导致异常,或者我是否需要循环直到我可以成功读取?