I have the following code which I want to tell me whenever a new file is added to the directory the FileSystemWatcher is monitoring. The problem is, when a file is created within the directory being monitored, it raises three events- I only want one. The events raised are all “changed”. Is there something I am doing incorrectly?
FileSystemWatcher _fileWatcher = new FileSystemWatcher();
_fileWatcher.Path = DirToMonitor;
_fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
_fileWatcher.Filter = "*.*";
_fileWatcher.Created += new FileSystemEventHandler(_fileWatcher_Created);
_fileWatcher.Renamed += new RenamedEventHandler(_fileWatcher_Renamed);
_fileWatcher.Changed += new FileSystemEventHandler(_fileWatcher_Changed);
_fileWatcher.EnableRaisingEvents = true;
EDIT: I used “LastWrite” because this is being monitored at the directory-level, not the new file. The only possible changes seemed to be LastWrite, Size and Attributes. LastWrite seemed the most sensible…
I am not entirely sure, but I would say it is down to your NotifyFilter. Try adjusting these to included other events and that should give you what you need
For more information : NotifyFilters enumerarion