首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileSystemWatcher不工作

FileSystemWatcher不工作
EN

Stack Overflow用户
提问于 2010-07-31 12:10:13
回答 2查看 6.1K关注 0票数 2

我像这样在Form1_Load中添加了FileSystemWatcher

代码语言:javascript
复制
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ....................
        Dim watcher As New FileSystemWatcher()
        'For watching current directory
        watcher.Path = "/"
        'For watching status.txt for any changes
        watcher.Filter = "status.txt"
        watcher.NotifyFilter = NotifyFilters.LastWrite
        watcher.EnableRaisingEvents = True
        AddHandler watcher.Changed, AddressOf OnChanged
End Sub

我有一个简单的MessageBox的OnChanged函数。尽管如此,当我更改status.txt文件时,没有显示任何消息框。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-31 12:55:55

代码语言:javascript
复制
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim watcher As New IO.FileSystemWatcher()

'For watching current directory
watcher.Path = **System.IO.Directory.GetCurrentDirectory()** 'Note how to obtain current directory
watcher.NotifyFilter = NotifyFilters.LastWrite

'When I pasted your code and created my own status.txt file using 
'right click->new->Text File on Windows 7 it appended a '.txt' automatically so the
'filter wasn't finding it as the file name was status.txt.txt renaming the file
'solved the problem
watcher.Filter = "status.txt" 

AddHandler watcher.Changed, AddressOf OnChanged

watcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
MessageBox.Show("Got it")
End Sub

来自http://bartdesmet.net/blogs/bart/archive/2004/10/21/447.aspx

您可能会注意到,在某些情况下,单个创建事件会生成多个由您的组件处理的已创建事件。例如,如果使用FileSystemWatcher组件监视目录中新文件的创建,然后使用记事本创建文件进行测试,则即使只创建了一个文件,也可能会生成两个已创建事件。这是因为记事本在写入过程中执行多个文件系统操作。记事本分批写入磁盘,创建文件内容,然后创建文件属性。其他应用程序也可以以相同的方式执行。因为FileSystemWatcher监视操作系统活动,所以这些应用程序触发的所有事件都将被拾取

票数 5
EN

Stack Overflow用户

发布于 2010-07-31 12:18:18

您还应该侦听已删除的事件。

根据您使用的编辑器,他们有时会删除/替换文件,而不是简单地更改它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3376763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档