Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using the following code to monitor a folder using Filesystemwatch
VB
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

VB
Private Sub Process(ByVal Source As Object, ByVal evt As FileSystemEventArgs)
'To do...
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

VB
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.
Posted
Updated 20-Mar-19 10:22am

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900