2017-03-24 4 views
2

FileSystemWatcherを作成する際には、どのNotifyFiltersを視聴するかを選択するオプションがあります。ただし、やNotifyFilters.Attributesなど、FileSystemWatcher.Changedイベントをトリガーする可能性のある複数のNotifyFiltersがあります。どのNotifyFilterがFileSystemWatcher.Changedをトリガしましたか?

コード:

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の作成について考えていたが、それはメモリの非常に効率的に使用するように見えるしていません。私はもっ​​ときれいな方法があることを望んでいた。

答えて

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; 

は\変更\は、名前を変更\を削除しました。ご覧のように、どのフィルタが変更を引き起こしたかについての情報はありません。

関連する問題