一个关于FileSystemWatcher的问题,谢谢
Hi,大家好,我在用FileSystemWatcher监视某个目录的时候出现了问题,在往被监视的目录拷贝文件的时候Create事件似乎不能被触发(请看下面的程序),我把一个txt文件从C盘的根目录拷贝到C:\ABC目录下,但是并没有触发事件,弹出消息框,请问是怎么回事,谢谢!-------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim watcher As New FileSystemWatcher
watcher.Path = "C:\ABC"
watcher.NotifyFilter = (NotifyFilters.Attributes Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime Or NotifyFilters.Size)
watcher.Filter = "*.txt"
AddHandler watcher.Created, AddressOf OnChanged
'AddHandler watcher.Changed, AddressOf OnChanged
'AddHandler watcher.Deleted, AddressOf OnChanged
'AddHandler watcher.Renamed, AddressOf OnRenamed
watcher.EnableRaisingEvents = True
End Sub
Sub OnChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
MessageBox.Show("test")
End Sub --------------------编程问答-------------------- 啊?CSDN现在人气这么差啊?都没有关注一下? --------------------编程问答-------------------- Dim watcher As New FileSystemWatcher
这是函数内部变量,随着函数的结束,而被回收了
把它定义为类变量
--------------------编程问答-------------------- http://topic.csdn.net/u/20071216/11/a79d510d-195d-43bc-b92a-bf5dbf72ad31.html --------------------编程问答-------------------- http://topic.csdn.net/u/20071216/11/a79d510d-195d-43bc-b92a-bf5dbf72ad31.html --------------------编程问答-------------------- 谢谢hdt的回复
现在我将程序修改为:
---------------------
Public watcher As FileSystemWatcher
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
watcher = New FileSystemWatcher
watcher.Path = "C:\ABC"
watcher.NotifyFilter = (NotifyFilters.Attributes Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime Or NotifyFilters.Size)
watcher.Filter = "*.txt"
AddHandler watcher.Created, AddressOf OnChanged
'AddHandler watcher.Changed, AddressOf OnChanged
'AddHandler watcher.Deleted, AddressOf OnChanged
'AddHandler watcher.Renamed, AddressOf OnRenamed
watcher.EnableRaisingEvents = True
End Sub
Sub OnChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
MessageBox.Show("test")
End Sub
---------------------
但是还是没有反应,messagebox还是不出来 --------------------编程问答-------------------- 我又试了一下,按照MSDN上写控制台程序就可以,但是现在我需要用窗体程序啊。请问怎么解决
补充:.NET技术 , C#