C#FileSystemWatcher事件未触发

C#FileSystemWatcher事件未触发,c#,event-handling,filesystemwatcher,C#,Event Handling,Filesystemwatcher,我正在尝试实现一个文件系统监视程序。但是,我的OnChange事件处理程序从未被调用。监视程序应该监视进程中另一个线程正在更新的日志文件。使用新StreamWriter打开文件(file.Open(“C:\\temp\\myLog.txt”,FileMode.Create,FileAccess.Write,FileShare.Read))

我正在尝试实现一个
文件系统监视程序
。但是,我的OnChange事件处理程序从未被调用。监视程序应该监视进程中另一个线程正在更新的日志文件。使用新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();
    }
}
public MyFormConstructor()
{
初始化组件();
这是。_fileSystemWatcher=new fileSystemWatcher();
这是。_fileSystemWatcher.NotifyFilter=NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;
这是。_fileSystemWatcher.Path=“C:\\temp\\”;
这个文件名为.u fileSystemWatcher.Filter=“myLog.txt”;
this.\u fileSystemWatcher.Changed+=this.OnLogChanged;
this.\u fileSystemWatcher.Created+=this.OnLogChanged;
this.\u fileSystemWatcher.Deleted+=this.OnLogChanged;
this.\u fileSystemWatcher.rename+=this.OnLogChanged;
这是。_fileSystemWatcher.EnableRaisingEvents=true;
}
私有void OnLogChanged(对象源、文件系统目标)
{

switch(e.ChangeType)//我在释放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);
        }
    }
}

代码工作。我在“never Get here”上设置了一个调试停止,创建了该文件,代码也停止了。这里也是。您如何确认OnLogChanged()没有被调用吗?我正在使用断点来确认。文件是否由同一进程更新有关系?我知道文件正在更新,因为我可以在记事本++中打开它并查看更新。@Chuchěxěŕ这很不幸。我知道记事本++不是C(可能是Win32 C++)但是我不知道他们是怎么做的。我不在调用本地方法。PThis是文件系统对象;它监视文件系统(不需要爬回到树上,诉诸C++;相同的OS对象)。。无论我是从同一个进程还是从另一个进程更新文件,我的代码副本都会命中断点。您所问的这些路径是否是源代码中使用的实际文字逐字字符串值,已复制而未重新键入?