FileSystemWatcher Fires OnChanged Event Multiple times

Posted on 2005-04-14
Last Modified: 2008-01-09
I have an .xml file that I am using as a configuration file that is loaded during application startup.  I have created a FileSystemWatcher object and set it to filter the FileName and LastWrite .xml file to filter on Changed and Rename events.  When the file is renamed, a message is popped and the application is terminated.  If the file is edited and saved I call the subs to reload the .xml file.  The rename changetype event operates correctly - it fires only once.  However when the file is edited and saved, the Changed event is fired three times.  I have coded around the problem but am not confident in whether it will always work as intended - why three times and will it always be three times on all computers? Can't find anything on M$ site about the potential cause of this!

Code extract follows:

Public Module Startup
    Private intChanged As Integer = 0
    Public Sub Main()
        ' Create FileSystemWatcher and set properties
        Dim watcher As New FileSystemWatcher
        watcher.Path = Application.StartupPath
        ' Watch for changes in LastWrite times and file rename
        watcher.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
        'Only watch the app config file
        watcher.Filter = "Config.xml"
        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Deleted, AddressOf OnChanged
        AddHandler watcher.Renamed, AddressOf OnRenamed
        ' Begin watching.
        watcher.EnableRaisingEvents = True
        LoadConfig(Application.StartupPath & "\" & Config.xml)
       '.... load the GUI
    End Sub

    Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' Execute when file is changed.
        intChanged += 1
       ' Msgbox(Cstr(e.ChangeType) & " event fired " & intChanged & " time(s)") ' For info
        If intChanged = 3 Then
        intChanged = 0
            LoadConfig(e.FullPath)  'Sub to reload the .xml file
        End If
    End Sub

    Private Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
        ' Exit the application if the config file is renamed
        MsgBox("File: " & e.OldName & " was renamed to " & e.Name & "!" & _
            vbCrLf & "This file is a required application file.  The " & _
            "application will terminate.", MsgBoxStyle.Critical, "File System Error")
    End Sub

    Private Sub LoadConfig(strXMLFile as String)
        'Xml file loading & parsing stubs
    End Sub

End Module
Question by:FlurbSnarf
    1 Comment
    LVL 85

    Accepted Solution

    From the help on the FileSystemWatcher:

        "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, anti-virus software) might cause additional file system events that are detected by FileSystemWatcher."

    In answer to your question:

    >> I have coded around the problem but am not confident in whether it will always work as intended - why three times and will it always be three times on all computers?

    So you can't be sure of the number of times it will fire.  I'm not sure of the best way to handle this other than to reload the file each time it fires.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now