Calseon
Calseon

Reputation: 361

Which NotifyFilter triggered FileSystemWatcher.Changed?

When creating a FileSystemWatcher, we have the option to select which NotifyFilters to watch for. To my understanding however, there are multiple NotifyFilters that could trigger the FileSystemWatcher.Changed event, such as NotifyFilters.LastWrite or NotifyFilters.Attributes.

Code:

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
}

Problem: In my event handler for the FileSystemWatcher.Changed event, is there a way to determine which NotifyFilter raised the event?

Attempt: I was thinking about just creating a new FileSystemWatcher for each type of NotifyFilter, but that doesn't seem like a very efficient use of memory. I was hoping there'd be a cleaner method.

Upvotes: 2

Views: 1289

Answers (1)

Evk
Evk

Reputation: 101553

No, there is no way to find this out, because underlying windows api which is called by FileSystemWatcher does not provide such information. Api which is called is ReadDirectoryChangesW and it returns result in FILE_NOTIFY_INFORMATION structure with the following fields:

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

Where action is created\modified\deleted\renamed. As you see - no information about what filter has triggered the change.

Upvotes: 2

Related Questions