FileSystemWatcher Changed event is raised twice

前端 未结 30 3700
死守一世寂寞
死守一世寂寞 2020-11-22 06:01

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event

30条回答
  •  余生分开走
    2020-11-22 06:32

    This solution worked for me on production application:

    Environment:

    VB.Net Framework 4.5.2

    Set manually object properties: NotifyFilter = Size

    Then use this code:

    Public Class main
        Dim CalledOnce = False
        Private Sub FileSystemWatcher1_Changed(sender As Object, e As IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
                If (CalledOnce = False) Then
                    CalledOnce = True
                    If (e.ChangeType = 4) Then
                        ' Do task...
                    CalledOnce = False
                End If
            End Sub
    End Sub
    

提交回复
热议问题