
So that is consistent with Notepad replacing the file contents with the Save menu item. Replacing the contents to the file (ie no append), OnFileChanged event fires two times for a write to the file.
FILEWATCHER NOTIFYFILTERS UPDATE
Update 2: Using StreamWriter to update the text file, it makes a difference whether you APPEND to the file contents or REPLACE the file contents.Īppending to the file, OnFileChanged event fires once for a write to the file. So it makes be wonder if Notepad is indeed performing more than one write/save operation when I click the Notepad Save menu item.


And the OnFileChanged event only fires one time for each text file change.
FILEWATCHER NOTIFYFILTERS CODE
I tried updating the text file from code using. String aFilepath = Now I'm starting to think this may be a MS Notepad issue. Private void OnFileChanged(object source, FileSystemEventArgs e)Ĭonsole.WriteLine("OnFileChanged" + ctr) Watcher.Changed += new FileSystemEventHandler(OnFileChanged) Watcher.NotifyFilter = NotifyFilters.LastWrite Here is my code: watcher new FileSystemWatcher() watcher.Path ConfigurationManager.AppSettingsWatchFolder watcher.NotifyFilter NotifyFilters. String fileName = Path.GetFileName(path) String folderName = Path.GetDirectoryName(path) Private void FileWatcherSetup(string path)įileSystemWatcher watcher = new FileSystemWatcher() So maybe we can hold this issue open for a while? A coder reading this could have an idea to help. I need to investigate more time to explore behavior of code in a Windows service. The above works for copied and moved folders (using instead, NotifyFilters.DirectoryName), although more code is required to trigger events for any files inside the folder. Maybe it has something to do with Windows messaging on specific servers. So I guess I am not the only person having trouble with this. This is from the documentation of the class. I am afraid that this is a well-known bug/feature of the FileSystemWatcher class. I noticed this behaviour years ago to face this again now. You should be able to filter out changes to the files security. But, looking into the documentation a little bit, you can set NotifyFilters on the FileSystemWatcher class. The main difference is that this this custom filewatcher class is implemented in a Windows service executable. I disclaim that I didnt even know about that class. In fact I am getting System.IO.IOExceptions at on our production servers.

I was not able to reproduce this behavior on my machine. Just tried to create a reproducable example but failed 😞.
