Search code examples
c#filesystemwatcher

FileSystemWatcher getting too many event


I'm trying to implement a file watcher that will raise an event if the file content was changed. The problem that once the file I am watching was modified, I am getting 2 events. (I want to get it only once)

_automationStatusWatcher = new FileSystemWatcher(fileInfo.Directory.FullName,
                                                 fileInfo.Name);
_automationStatusWatcher.NotifyFilter = NotifyFilters.LastWrite;
_automationStatusWatcher.Changed += OnAutomationStatusChanged;
_automationStatusWatcher.EnableRaisingEvents = true;

The file that i'm watching is not intended to be recreated/moved/deleted/whatever. its purpose is to be an xml database file that i need to monitor it once it changes. i want to get only 1 event when the file is modified and to ignore the other events.

How can I do that?


Solution

  • Manual:

    Note

    Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.