FileSystemWatcher Changed event is raised twice

前端 未结 30 3798
死守一世寂寞
死守一世寂寞 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:19

    I was able to do this by added a function that checks for duplicates in an buffer array.

    Then perform the action after the array has not been modified for X time using a timer: - Reset timer every time something is written to the buffer - Perform action on tick

    This also catches another duplication type. If you modify a file inside a folder, the folder also throws a Change event.

    Function is_duplicate(str1 As String) As Boolean
        If lb_actions_list.Items.Count = 0 Then
            Return False
        Else
            Dim compStr As String = lb_actions_list.Items(lb_actions_list.Items.Count - 1).ToString
            compStr = compStr.Substring(compStr.IndexOf("-") + 1).Trim
    
            If compStr <> str1 AndAlso compStr.parentDir <> str1 & "\" Then
                Return False
            Else
                Return True
            End If
        End If
    End Function
    
    Public Module extentions
    
    Public Function parentDir(ByVal aString As String) As String
        Return aString.Substring(0, CInt(InStrRev(aString, "\", aString.Length - 1)))
    End Function
    End Module
    

提交回复
热议问题