Hi all
I have to write an application in VB.NET which monitors multiple files
at the time and notifies the user about changes. I thought to use
multiple instances of FileSystemWatcher in an array (the number of
instances i need is dynamic). Here is the code I wrote:
Dim arrFSW() As FileSystemWatcher
ReDim arrFSW(lstFiles.Items.Count - 1)
For i As Integer = 0 To lstFiles.Items.Count - 1
strFile = lstFiles.Items(i)
arrFSW(i) = New FileSystemWatcher
arrFSW(i).Path = "mypath"
arrFSW(i).Filter = "myfile"
arrFSW(i).NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName Or
NotifyFilters.DirectoryName)
AddHandler arrFSW(i).Changed, AddressOf FSWChanged
AddHandler arrFSW(i).Deleted, AddressOf FSWDeleted
AddHandler arrFSW(i).Renamed, AddressOf FSWRenamed
arrFSW(i).EnableRaisingEvents = True
Next
Private Shared Sub FSWChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Console.WriteLine("File: " & e.Name & " changed")
End Sub
Private Shared Sub FSWDeleted(ByVal source As Object, ByVal e As
FileSystemEventArgs)
Console.WriteLine("File: " & e.Name & " deleted")
End Sub
Private Shared Sub FSWRenamed(ByVal source As Object, ByVal e As
RenamedEventArgs)
Console.WriteLine("File: {0} renamed to {1}", e.OldName, e.Name)
End Sub
Now, the code does not raise any compilation error, but the
FileSystemWatcher does not raise the events since the array cannot be
declared as WithEvents. Does anybody have a solution for this issue?
Any help is greatly appreciated!
Greets
DiGa