处理 FileSystemWatcher 中的多个更改事件
我有以下子内容:
Private Sub Watcher_Changed(ByVal sender As System.Object, ByVal e As FileSystemEventArgs)
If Path.GetExtension(e.Name) = ".p2p" Then
Exit Sub
Else
Try
' multiple change events can be thrown. Check that file hasn't already been moved.
While Not File.Exists(e.FullPath)
Exit Try
End While
' throw further processing to a BackGroundWorker
ChangedFullPath = e.FullPath
ChangedFileName = e.Name
FileMover = New BackgroundWorker
AddHandler FileMover.DoWork, New DoWorkEventHandler(AddressOf ProcessFile)
FileMover.RunWorkerAsync()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
当通过 FTP 上传文件时,我仍然收到多个文件更改通知。
我想修改 Try,这样如果更改通知发生在过去(时间)内(比方说 3 秒),它也会抛出更改通知。 这本来应该是微不足道的,但由于某种原因,我今天没有想到它,而且我的思绪也没有集中在我在谷歌上找到的答案上。
谢谢, 斯科特
I have the following sub:
Private Sub Watcher_Changed(ByVal sender As System.Object, ByVal e As FileSystemEventArgs)
If Path.GetExtension(e.Name) = ".p2p" Then
Exit Sub
Else
Try
' multiple change events can be thrown. Check that file hasn't already been moved.
While Not File.Exists(e.FullPath)
Exit Try
End While
' throw further processing to a BackGroundWorker
ChangedFullPath = e.FullPath
ChangedFileName = e.Name
FileMover = New BackgroundWorker
AddHandler FileMover.DoWork, New DoWorkEventHandler(AddressOf ProcessFile)
FileMover.RunWorkerAsync()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
I'm still getting multiple changed-file notifications when a file is being uploaded by FTP.
I want to modify the Try so it also throws out the change notification if it has happened within the past (time) -- let's say 3 seconds. It ought to be trivial, but for some reason it isn't coming to me today, and my mind isn't wrapping itself around the answers I'm finding on Google.
Thanks,
Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
几年前,我创建了一项服务来上传转储到 FTP 文件夹的文件,以下是我所做的一些事情,应该可以帮助您解决问题:
这似乎对我来说很有效,并且该服务可靠地上传文件几个月,直到我离开那份工作。
I created a service to upload files dumped to an FTP folder a couple years ago and here's some things I did that should help you get over your problem:
This seemed to work out for me and that service ran reliably uploading files for months until I left that job.
我们也有类似的情况; 然而,批量写入文件的系统使用了临时名称,并在完成后将其更改为“真实”名称。 你的 ftp 客户端可以做类似的事情吗?
如果可以,那么您可以检查新文件名并通过扩展名或前缀进行检查; 如果它不是预期的最终文件名格式,那么您可以忽略它。
we had a similar situation; however the system that wrote the file in batches used a temp name, and changed it to the "real" name when it was done. Can your ftp client do something similar?
If it can, then you can check the new filename and check it by extension or a prefix; if its not the expected final filename format then you can ignore it.
我之前遇到过确切的情况,最终实现了一个计时器机制来等待文件“稳定”,或者等待写入事件停止进入 x 时间。
有点笨拙,但它有效。
I ran into the exact situation before, and ended up implementing a timer mechanism to wait for the file to "settle", or for write events to stop coming in for x amount of time.
A bit kludgy, but it worked.
需要注意的一件事是,访问文件可以更改文件属性之一,例如“访问时间”或其他内容。 如果您没有正确设置通知过滤器,则可能会触发另一个事件。
One thing to note is that accessing the file can change one of the file properties, like "accessed time" or something. That can then fire off another event if you don't have the notification filters setup right.
我遇到了同样的问题,我正在保存一个通过代码上传的文件,删除和创建每次都会触发两次。 我所需要检查的是在进行处理之前该文件是否存在。
I had this same issue, I was saving a file that was uploaded through code and deleting and creating would fire twice each time. All I had to check was that the File was there before doing my processing.
我今天在尝试在更改时自动重新加载配置文件时遇到了同样的问题。 我最终计算了文件的 MD5 哈希值,以确定文件是否确实已更改,并且只有在哈希值不同时才重新加载。 这是我过滤掉重复项的唯一确定方法。
我短暂地考虑过使用计时器机制来过滤掉更改通知,但感觉就像 pete-m 一样,但我觉得这是一个太不干净的解决方案。
我尝试仅查看 LastWrite 时间更改,但这不起作用。 我仍然收到重复的通知。
I had the same problem today while trying to automatically reload a configuration file on change. I ended up computing an MD5 hash of the file to figure out if the file had actually changed and only reloading if the hash was different. This was the only definitive way I could filter out duplicates.
I briefly thought about using a timer mechanism to filter out change notifications but felt, like pete-m, but I felt it was too unclean a solution.
I tried watching only for LastWrite time changes but that didn't work. I was still getting duplicate notifications.
我试图检测 Excel 文件何时发生更改,但遇到了一个问题,即更改的事件未触发,并且因为我只关心单个文件设置,所以过滤器不起作用。 当我删除过滤器并查看触发的事件时,事件参数名称属性看起来是随机生成的名称,而不是我知道已更改的特定文件名。
因此,当特定 Excel 文件发生更改时,我无法收到简单的通知。
I'm trying to detect when an excel file changes but am running into a problem that the changed event is not firing and as Im only concern with a single file setting the filter doesnt work. When I remove the filter and look at an event that is triggered the event argument name property looks to be a random generated name and not the specific filename which I know has changed.
So I'm not able to get a simple notification when a specific excel file changes.