为什么 FileSystemWatcher 会触发两次?有没有简单的方法来修复它?当然,如果我更新或编辑文本文件,它应该只触发一次?
这个链接在这里http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx说
- 引发两次的事件 - 如果显式指定事件处理程序(AddHander FSW.Created、AddressOf FSW_Created),则将引发两次事件。这是因为默认情况下,公共事件会自动调用相应的受保护方法(OnChanged、OnCreated、OnDeleted、OnRenamed)。要纠正此问题,只需删除显式事件处理程序 (AddHandler ...)。
“删除显式事件处理程序”是什么意思?
Imports System.IO
Public Class Form2
Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
'this fires twice
MessageBox.Show("test")
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileSystemWatcher1.Path = "C:\Users\c\Desktop\test\"
FileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.CreationTime
FileSystemWatcher1.IncludeSubdirectories = False
FileSystemWatcher1.Filter = "text.txt"
End Sub
End Class