FileSystemWatcher array WithEvents

D

DiGa

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
 
D

DiGa

Hi everybody,

Sorry for anoying you. I found the problem which lays in wrong value
for the path variable. Problem solved.

Thank you!
 
L

Larry Lard

DiGa said:
Hi everybody,

Sorry for anoying you. I found the problem which lays in wrong value
for the path variable. Problem solved.

Thank you!

Good cos that one had me stumped :) From the subject line I was
expecting to have to explain about AddHandler and so forth but you
clearly already knew that...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top