2

This is my code:

 _instrumentsFolderWatcher.Changed += _instrumentsFolderWatcher_Changed;
 _instrumentsFolderWatcher.Created += _instrumentsFolderWatcher_Created;
 _instrumentsFolderWatcher.Deleted += _instrumentsFolderWatcher_Deleted;

where _instrumentsFolderWatcher is instance of FileSystemWatcher. Problem is when I paste a file in the folder which is being monitored, both the created and changed events are fired. Why would changed event be fired? Further the changed event is fired with ChangeType of Changed. How do I get around this issue?

Edit: Why is Changed event not fired when I change the filename?

Jack
  • 7,433
  • 22
  • 63
  • 107
  • When you "paste" the file, the new file is both being created and changed. Can you explain how this causes an issue for you? – William Oct 17 '13 at 03:46
  • Hope this help: http://stackoverflow.com/questions/449993/vb-net-filesystemwatcher-multiple-change-events – Tu Tran Oct 17 '13 at 03:47
  • @William: If it is so obvious, next question is why would changed event be fired 2 times? – Jack Oct 17 '13 at 04:11
  • 1
    @Jack: I didn't intend to indicate that anything was obvious, only that if I had a better understanding of why multiple events caused you trouble I may be able to suggest a way to mitigate the issue. Changing the file name should result in a `Renamed` event. You may find the documentation at [File System Watcher Class](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) useful. Specifically the section in Remarks titled "Events and Buffer Sizes" – William Oct 17 '13 at 04:25

1 Answers1

2

MSDN FileSystemWatcher Class

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.

Try using the Renamed event to determine when a file is renamed.

Jethro
  • 5,896
  • 3
  • 23
  • 24