哪个 NotifyFilter 触发了 FileSystemWatcher.Changed?
Which NotifyFilter triggered FileSystemWatcher.Changed?
创建 FileSystemWatcher
时,我们可以选择 select 要监视哪个 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
引发了事件?
尝试: 我正在考虑为每种类型的 NotifyFilter
创建一个新的 FileSystemWatcher
,但这似乎不是一个非常有效的方法内存的使用。我希望有一个更清洁的方法。
不,没有办法找到它,因为 FileSystemWatcher
调用的底层 windows api 不提供此类信息。调用的 Api 是具有以下字段的 ReadDirectoryChangesW and it returns result in FILE_NOTIFY_INFORMATION 结构:
typedef struct _FILE_NOTIFY_INFORMATION {
DWORD NextEntryOffset;
DWORD Action;
DWORD FileNameLength;
WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
行动是created\modified\deleted\renamed。如您所见 - 没有关于触发更改的过滤器的信息。
创建 FileSystemWatcher
时,我们可以选择 select 要监视哪个 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
引发了事件?
尝试: 我正在考虑为每种类型的 NotifyFilter
创建一个新的 FileSystemWatcher
,但这似乎不是一个非常有效的方法内存的使用。我希望有一个更清洁的方法。
不,没有办法找到它,因为 FileSystemWatcher
调用的底层 windows api 不提供此类信息。调用的 Api 是具有以下字段的 ReadDirectoryChangesW and it returns result in FILE_NOTIFY_INFORMATION 结构:
typedef struct _FILE_NOTIFY_INFORMATION {
DWORD NextEntryOffset;
DWORD Action;
DWORD FileNameLength;
WCHAR FileName[1];
} FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
行动是created\modified\deleted\renamed。如您所见 - 没有关于触发更改的过滤器的信息。