C# Application FileSystemWatcher to handle replaced files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xMetalDetectorx
    New Member
    • Oct 2008
    • 44

    C# Application FileSystemWatcher to handle replaced files

    I am using FileSystemWatch er to monitor a directory for files that are created and replaced. It is working fine for new files, but if I copy a file into the directory with the same name and choose to replace the existing file, it doesn't trigger any event.

    Does anyone know how to get that specific event? (Replace a file)

    Thanks.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I would expect that it triggers the 'Changed' event. I see that every time I add a line to a text log file. There is a change in the 'last access time' so the file has a change.

    I would expect that totally replacing the file must register as a change.

    Comment

    • xMetalDetectorx
      New Member
      • Oct 2008
      • 44

      #3
      I have tried making the NotifyFilter = LastWrite, and added an event handler for Changed, but that is still not picking up replaced files (The new files have new Date Modified values).

      Code:
      oFileWatcher.NotifyFilter = NotifyFilters.LastWrite;
      
      oFileWatcher.Deleted += new System.IO.FileSystemEventHandler(Deleted);
      oFileWatcher.Created += new System.IO.FileSystemEventHandler(Created);
      oFileWatcher.Changed += new System.IO.FileSystemEventHandler(Changed);
      Any other ideas?

      Comment

      • xMetalDetectorx
        New Member
        • Oct 2008
        • 44

        #4
        I got it to work with the above code, but for every replaced file, it fires 3 Changed events.....

        Comment

        • andyuk0000000000
          New Member
          • Oct 2008
          • 5

          #5
          It's the Chaged event that gets fired in this scenario, have set EnableRaisingEv ents to true ?

          Comment

          • andyuk0000000000
            New Member
            • Oct 2008
            • 5

            #6
            This is as described in MSDN, I'd guess your getting one for each attribute that changes. Try changing the NotifyFilter to get just the one. I think you'll always get more than one for a replace though.

            Comment

            Working...