C# FileSystemWatcher 事件未触发
C# FileSystemWatcher Events Not Firing
我正在尝试实施 FileSystemWatcher
。但是,我的 OnChange 事件处理程序永远不会被调用。观察者应该监视正在由进程中的另一个线程更新的日志文件。使用 new StreamWriter(File.Open("C:\temp\myLog.txt", FileMode.Create, FileAccess.Write, FileShare.Read));
打开文件有什么想法吗?
public MyFormConstructor()
{
InitializeComponent();
this._fileSystemWatcher = new FileSystemWatcher();
this._fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
this._fileSystemWatcher.Path = "C:\temp\";
this._fileSystemWatcher.Filter = "myLog.txt";
this._fileSystemWatcher.Changed += this.OnLogChanged;
this._fileSystemWatcher.Created += this.OnLogChanged;
this._fileSystemWatcher.Deleted += this.OnLogChanged;
this._fileSystemWatcher.Renamed += this.OnLogChanged;
this._fileSystemWatcher.EnableRaisingEvents = true;
}
private void OnLogChanged(object source, FileSystemEventArgs e)
{
switch (e.ChangeType) // <-- never gets here
{
case WatcherChangeTypes.Changed:
this.UpdateLogView();
break;
case WatcherChangeTypes.Created:
case WatcherChangeTypes.Deleted:
case WatcherChangeTypes.Renamed:
default:
throw new NotImplementedException();
}
}
我在处理 StreamWriter 时收到文件系统事件,但之前没有。
所以处理它。
public class FlushingTextTraceListener : TextWriterTraceListener
{
public FlushingTextTraceListener(string filePath)
{
FilePath = filePath;
}
public String FilePath { get; set; }
public override void Write(string message)
{
using (var sw = new StreamWriter(File.Open(FilePath, FileMode.Create,
FileAccess.Write, FileShare.Read)))
{
sw.Write(message);
}
}
public override void WriteLine(string message)
{
using (var sw = new StreamWriter(File.Open(FilePath, FileMode.Create,
FileAccess.Write, FileShare.Read)))
{
sw.WriteLine(message);
}
}
}
我正在尝试实施 FileSystemWatcher
。但是,我的 OnChange 事件处理程序永远不会被调用。观察者应该监视正在由进程中的另一个线程更新的日志文件。使用 new StreamWriter(File.Open("C:\temp\myLog.txt", FileMode.Create, FileAccess.Write, FileShare.Read));
打开文件有什么想法吗?
public MyFormConstructor()
{
InitializeComponent();
this._fileSystemWatcher = new FileSystemWatcher();
this._fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
this._fileSystemWatcher.Path = "C:\temp\";
this._fileSystemWatcher.Filter = "myLog.txt";
this._fileSystemWatcher.Changed += this.OnLogChanged;
this._fileSystemWatcher.Created += this.OnLogChanged;
this._fileSystemWatcher.Deleted += this.OnLogChanged;
this._fileSystemWatcher.Renamed += this.OnLogChanged;
this._fileSystemWatcher.EnableRaisingEvents = true;
}
private void OnLogChanged(object source, FileSystemEventArgs e)
{
switch (e.ChangeType) // <-- never gets here
{
case WatcherChangeTypes.Changed:
this.UpdateLogView();
break;
case WatcherChangeTypes.Created:
case WatcherChangeTypes.Deleted:
case WatcherChangeTypes.Renamed:
default:
throw new NotImplementedException();
}
}
我在处理 StreamWriter 时收到文件系统事件,但之前没有。
所以处理它。
public class FlushingTextTraceListener : TextWriterTraceListener
{
public FlushingTextTraceListener(string filePath)
{
FilePath = filePath;
}
public String FilePath { get; set; }
public override void Write(string message)
{
using (var sw = new StreamWriter(File.Open(FilePath, FileMode.Create,
FileAccess.Write, FileShare.Read)))
{
sw.Write(message);
}
}
public override void WriteLine(string message)
{
using (var sw = new StreamWriter(File.Open(FilePath, FileMode.Create,
FileAccess.Write, FileShare.Read)))
{
sw.WriteLine(message);
}
}
}