这没有任何意义,但是,如标题所述,当在 FileSystemWatcher 监视的文件夹中使用 Beyond Compare 修改文件时,不会触发 Changed 事件。
当使用 Notepad++ 修改同一文件时,会触发 Changed 事件。 我确实验证了在 Beyond Compare 中保存时文件实际上已更改,因为我在使用 Beyond Compare 保存后在记事本中打开了它,并且更改已经存在。
有人遇到过这种奇怪的情况吗?有什么想法为什么会发生这种情况以及如何在从 Beyond Compare 保存时触发 Changed 事件吗?
与 IMO 无关的标准 FSW 代码:
fswDir = new FileSystemWatcher
{
Path = dirPath,
NotifyFilter = NotifyFilters.LastWrite,
Filter = "*.*",
EnableRaisingEvents = true
};
private void OnFswDirf_Changed(object sender, FileSystemEventArgs e)
{
var filePath = e.FullPath;
// Not executed when file saved by BC, executed when file saved by Notepad++.
}
订阅所有 FSW 事件和所有 NotifyFilter 枚举后,我设法获取由 Beyond Compare 4 文件>保存功能触发的 FSW 事件的完整序列,以防将来有人需要:
经过排除过程后发现,为了获得 BC NotifyFilter 所做的更改,需要设置为:
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes;
此后 Changed 事件将捕获 BC 的文件更改。
这对我有用:
FileWatcher = new FileSystemWatcher();
FileWatcher.Path = folder;
FileWatcher.Filter = filename;
FileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
FileWatcher.Changed += FileWatcher_Changed;
FileWatcher.Renamed += FileWatcher_Changed; // BYC
FileWatcher.EnableRaisingEvents = true;
看起来BYC比较正在将文件写入其他地方,然后重命名。 Changed 对于大多数其他事情都有效,Renamed 是 BYC 所火的。