将FileSystemWatcher与指定的文件名一起使用

时间:2013-09-23 15:08:15

标签: vb.net file filesystemwatcher

如果使用FileSystemWatcher在例如“C:\”中创建具有指定名称的文件,是否有办法执行操作?

Private Sub FileSystemWatcher1_changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
    If 
         'really don't know what to put here
    End If
End Sub

如果你不理解,我会解释。

1 个答案:

答案 0 :(得分:3)

我们假设您已准备好FileSystemWatcher1这些属性

Dim FileSystemWatcher1 = New FileSystemWatcher()
FileSystemWatcher1.Path = "C:\"
FileSystemWatcher1.Filter = "*.*"
AddHandler FileSystemWatcher1.Created, AddressOf OnCreated
FileSystemWatcher1.EnableRaisingEvents = True
.....

然后你可以编写你上面已经完成的事件处理程序,并查看传递给事件处理程序的FileSystemEventArgs参数的属性,以了解创建的文件的确切名称。

Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
    If e.Name.ToUpper() == "MYTEXTFILE.TXT" then
        ' do you code here '
    End If
End Sub