Vb .NET多文件系统监视实例 [英] Vb .NET multiple filesystemwatch instances
问题描述
我正在使用以下代码使用Filesystemwatch监控文件夹
I'm using the following code to monitor a folder using Filesystemwatch
Dim foldertowatch as New FileSystemWatcher()
foldertowatch.Path = source_folder
With foldertowatch
'File type
.Filter = source_prefix & "*." & source_file_type
'Notify Filter
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
'To watch sub directories....
.IncludeSubdirectories = sub_folder
'Handle new file created
AddHandler .Created, AddressOf Process
.EnableRaisingEvents = True
End With
并使用以下子处理创建的事件
And using the below sub to handle the created event
Private Sub Process(ByVal Source As Object, ByVal evt As FileSystemEventArgs)
'To do...
End Sub
现在,我需要监控多个文件夹,每个文件夹具有不同的文件类型,以及需要执行的不同过程。例如
来源1 - 创建时监控pdf文件 - 待办事项:重命名并保存到目标1
源2 - 监控txt文件创建时 - 待办事项:重命名并保存到目标2
源3 - 创建时监控excel文件 - 待办事项:重命名并保存到目标3
等等。 ..
我尝试了什么:
我试图创建Filesystemwatch的多个实例
Now it comes to a point where I need to monitor multiple folders, each folder with different file types, and different processes need to do. For example
Source 1 - Monitor pdf file when created - To Do: Rename and save to Destination 1
Source 2 - Monitor txt file when created - To Do: Rename and save to Destination 2
Source 3 - Monitor excel file when created - To Do: Rename and save to Destination 3
and so on...
What I have tried:
I tried to created multi instances of Filesystemwatch
Dim foldertowatch(total_items) As FileSystemWatcher
For i = 0 To 10
foldertowatch(i) = New FileSystemWatcher()
foldertowatch(i).Path = source(i)
With foldertowatch(i)
'File type
.Filter = source_prefix(i) & "*." & source_file_type(i)
'Notify Filter
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
'To watch sub directories....
.IncludeSubdirectories = sub_folder(i)
'Handle new file created
AddHandler .Created, AddressOf Process
.EnableRaisingEvents = True
End With
Next
但我不知道如何处理所引发的事件来自不同的实例。基本上,我如何知道哪个实例引发事件以便继续重命名/保存到正确的文件夹。
But I don't know how to handle the events raised from different instances. Basically, How do I know which instance raises the event so that to proceed with rename/save to correct folder.
推荐答案
FileSystemEventArgs类有一个文件路径和一个名称属性。使用它们来确定受影响的文件。添加逻辑以确定文件类型/路径,并对每种文件类型或文件夹执行任何操作。
另请参阅: docs.microsoft.com上的FileSystemEventArgs
The FileSystemEventArgs class has a file path and a name property. Use those to determine which file is affected. Add logic to determine the file type/path and do whatever you want to do with each file type or folder.
See also: FileSystemEventArgs on docs.microsoft.com
这篇关于Vb .NET多文件系统监视实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!