哪个NotifyFilter触发了FileSystemWatcher.Changed?

时间:2017-03-24 21:18:08

标签: c# filesystemwatcher

创建FileSystemWatcher时,我们可以选择要注意的NotifyFilters。但是,根据我的理解,有多个NotifyFilters可能会触发FileSystemWatcher.Changed事件,例如NotifyFilters.LastWriteNotifyFilters.Attributes

代码:

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(PATH);
watcher.Filter = Path.GetFileName(PATH);
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.FileName | NotifyFilters.Security |
                        NotifyFilters.CreationTime | NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.DirectoryName;
watcher.Changed += OnFileSystemWatcher_Changed;
watcher.Deleted += OnFileSystemWatcher_Deleted;
watcher.Created += OnFileSystemWatcher_Created;
watcher.Renamed += OnFileSystemWatcher_Renamed;

private void OnFileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
    // Do Stuff
}

问题:FileSystemWatcher.Changed事件的事件处理程序中,有没有办法确定哪个NotifyFilter引发了事件?

尝试:我正在考虑为每种类型的FileSystemWatcher创建一个新的NotifyFilter,但这似乎不是一种非常有效的内存使用方式。我希望那里有一个更干净的方法。

1 个答案:

答案 0 :(得分:2)

不,没有办法找到它,因为FileSystemWatcher调用的基础窗口api不提供此类信息。调用的Api是ReadDirectoryChangesW,它返回FILE_NOTIFY_INFORMATION结构中的结果,并带有以下字段:

typedef struct _FILE_NOTIFY_INFORMATION {
  DWORD NextEntryOffset;
  DWORD Action;
  DWORD FileNameLength;
  WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;

创建操作的地方\已修改\已删除\已重命名。如您所见 - 没有关于哪个过滤器触发了更改的信息。