0

I want to monitor the following

  • New File being created/copied to the directory
  • Existing file edited

I use the following code to subscribe to the created and changed event of the FileSystemWatcher class.I have noted some issues with FSW Class.

  • On replacing files, the changed event is getting triggered numerous times.

How can i get over this issue.Kindly advice.

 watcher.Path = watchpath;
 watcher.Filter = "*.*";
 watcher.Created += new FileSystemEventHandler(copied);
 watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
 watcher.NotifyFilter = NotifyFilters.LastWrite;
 watcher.EnableRaisingEvents = true;

For a single item copied to the folder,the following events are raised

  *******> Created 
    -----> Changed 
    -----> Changed 
techno
  • 6,100
  • 16
  • 86
  • 192
  • Because multiple things changed. File contents. File last-modified time. etc. – Raymond Chen Mar 06 '19 at 03:42
  • @RaymondChen How can i fix this issue? Please advice. – techno Mar 06 '19 at 04:28
  • Wow! Seeing a comment on SO by RC. Love your blog. – jay.w Mar 06 '19 at 05:29
  • There is nothing to fix. The FileSystemWatcher is behaving as designed. The file changed twice. – Raymond Chen Mar 06 '19 at 05:35
  • @RaymondChen Well its not behaving as expected even after adding `watcher.NotifyFilter = NotifyFilters.LastWrite;` For 8 files,the event is triggered more than 8 times. – techno Mar 06 '19 at 05:39
  • Redundant notifications are within spec. The purpose of the FileSystemWatcher is to let you monitor a directory listing so you can maintain a cached copy. The event tells you to go refresh your cache. Redundant refresh requests are allowed. The system doesn't say "Oh, wait, somebody updated the last-write time, and it's the same as the previous last-write time, so I'll suppress the notification." If somebody updates the last-write time, it raises the event, even if they updated it to the same value it already had. – Raymond Chen Mar 06 '19 at 15:06

1 Answers1

0

Yes, that is expected as already mentioned in the comments. Also have a look at : FileSystemWatcher Changed event is raised twice

to have a workaround you will need to add a dictionary which will track the raised events for every files.

Dictionary<string, DateTime> lastWriteDate //fileName - Last write date time

and in changed event you will have to handle it like below,

    private static void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        string filePath = e.FullPath;
        DateTime writeDate = System.IO.File.GetLastWriteTime(filePath);
        if (lastWriteDate.ContainsKey(filePath))
        {
            if (lastWriteDate[filePath] == writeDate) 
                //Exit as we already have raised an event for this
                return;
            lastWriteDate[filePath] = writeDate;
        }
        else
        {
            lastWriteDate.Add(filePath, writeDate);
        }

        //Do your stuff.
    }
Amit
  • 1,821
  • 1
  • 17
  • 30
  • Thanks but for 8 files,the event is getting triggered 13 times. – techno Mar 06 '19 at 05:42
  • Can you implement this in your test code, I believe this should be able to "return" multiple events raise. Let me know the observation. – Amit Mar 06 '19 at 05:49
  • I managed to solve the issue by using this answer from the referred post https://stackoverflow.com/a/23287926/848968 – techno Mar 06 '19 at 06:15
  • @techno very well, kindly extract the answer from that Git Repo solution and post it here, who knows when that user just delete that repo. – Amit Mar 06 '19 at 06:18
  • This question has been flagged for duplicate. – techno Mar 06 '19 at 06:22