File Watcher

Hi,

I have a fileWatcher set as follow:

Private Sub setupFilewatcher()
Dim info As New System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
Dim path As String = info.DirectoryName
Dim file As String = info.Name

Try
watcher = New System.IO.FileSystemWatcher(path)
watcher.Filter = file
watcher.NotifyFilter = IO.NotifyFilters.LastWrite
watcher.EnableRaisingEvents = True

Catch e As Exception
'Do nothing...
End Try

End Sub

My problem is that the event watcher.changed is fired 4 times when a save occur in the file. I need to catch only 1 event and not 4 times the same event.

Any ints

Tanks

Sylvain




Answer this question

File Watcher

  • Ganeshm

    Hi

    The file system watcher notifies you each time the last write is changed (measured internally within a very small time frame) .... it just sounds like your config file is updated 4 times for some reason.

    I guess you need to implement some kind of internal filter if the event 'spamming' is undesired. Perhaps each time the event is raised, you reset some kind of flag and the date you changed it and have a background thread monitor it. If the file is changed and the timespan between now and the last updated time is greater than x seconds, do your work.

    It is perhaps an undesirable solution, but the FSW class is doing exactly what you have asked it to do.

    Good luck

    Richard


  • bonus

    The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.

    Note

    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.

    Use NotifyFilter to restrict the number of notifications raised when this event is handled.



  • Tryin2Bgood

    As you can see I use the lastWrite filter wich works fine.

    The problem is that while I test the code in debug. When I modified and save the file that is monitored, the changed event is fired about 4 times.

    I can not use the other filter because they don't apply.

    If there is no way to work with the FileWatcher, why would Microsoft made a class for it



  • ks2006

    I saw a comment on another site (sorry I don't remember where - but it exists in the VS2005 Help search utility). The following statement was generally helpful, " What I usually do is add the file path and file size to a list, and then wait a period of time. When the time has elapsed, I compare the current file size against the earlier file size. When the file size has not changed for two subsequent checks, I "Assume" the file is done."
  • File Watcher