VB.NET FileSystemWatcher的多个更改事件多个、事件、VB、NET

2023-09-04 00:05:04 作者:除草做尼玛

我有以下的code:



Imports System.IO

Public Class Blah
    Public Sub New()
        InitializeComponent()

        Dim watcher As New FileSystemWatcher("C:\")
        watcher.EnableRaisingEvents = True

        AddHandler watcher.Changed, AddressOf watcher_Changed
    End Sub

    Private Sub watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        MsgBox(e.FullPath)
    End Sub
End Class

当我运行它,并更改保存到一个文件在我的C盘,在code的伟大工程,但它执行watcher_Changed()方法四次。知道为什么吗?中ChangeType是4的每一次。

When I run it and save changes to a file on my C drive, the code works great, except it executes the watcher_Changed() method four times. Any idea why? The changeType is "4" every time.

感谢。

推荐答案

从VS.NET文档的疑难解答FileSystemWatcher的组件部分...

From the "Troubleshooting FileSystemWatcher Components" section of the VS.NET documentation...

您可能会注意到单个创建事件生成由组件来处理多个创建的事件某些情况下。例如,如果使用FileSystemWatcher组件来监控目录中创建新的文件,然后用记事本创建一个文件测试它,你可能会看到,即使是只创建一个单独的文件生成两个创建的事件。这是因为记事本进行在写作过程中多个文件系统操作。记事写入磁盘中所有创建该文件的内容,然后将文件属性批次。其他应用程序可以以相同的方式执行。由于FileSystemWatcher的监控操作系统的活动,所有的事件,这些应用火将有所回升。

Multiple Created Events Generated for a Single Action

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

请注意:记事本也可能导致其他有趣的事件代。例如,如果您使用ChangeEventFilter指定您希望只为属性的变化看,然后你写的,你用记事本看目录中的文件,你会引发一个事件。这是因为,记事更新该操作期间该文件的存档的属性

Note: Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.