创建FileSystemWatcher
时,我们可以选择要注意的NotifyFilters
。但是,根据我的理解,有多个NotifyFilters
可能会触发FileSystemWatcher.Changed
事件,例如NotifyFilters.LastWrite
或NotifyFilters.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
,但这似乎不是一种非常有效的内存使用方式。我希望那里有一个更干净的方法。
答案 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;
创建操作的地方\已修改\已删除\已重命名。如您所见 - 没有关于哪个过滤器触发了更改的信息。