I'm using the following code to monitor a folder using Filesystemwatch
Dim foldertowatch as New FileSystemWatcher()
foldertowatch.Path = source_folder
With foldertowatch
.Filter = source_prefix & "*." & source_file_type
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
.IncludeSubdirectories = sub_folder
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)
End Sub
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)
.Filter = source_prefix(i) & "*." & source_file_type(i)
.NotifyFilter = .NotifyFilter Or NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or NotifyFilters.DirectoryName
.IncludeSubdirectories = sub_folder(i)
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.