-2

I have a filesystemwatcher written from examples found here that is working fine, along the lines:

Public monitor As FileSystemWatcher
monitor = New System.IO.FileSystemWatcher()
monitor.Path = c:\temp\
monitor.NotifyFilter = IO.NotifyFilters.DirectoryName
monitor.NotifyFilter = monitor.NotifyFilter Or IO.NotifyFilters.FileName
monitor.NotifyFilter = monitor.NotifyFilter Or IO.NotifyFilters.Attributes

'Add handlers
AddHandler monitor.Changed, AddressOf fileevent
AddHandler monitor.Created, AddressOf fileevent
AddHandler monitor.Deleted, AddressOf fileevent

'Start watching
monitor.EnableRaisingEvents = True

What I'm struggling with is expanding on this to monitor multiple folders, but without knowing except at run time how many folders there will be to monitor.

This question seems to cover it in C# Multiple Configurable FileSystemWatcher methods

But my lack of experience is so far preventing me from managing to translate this to VB.NET

Community
  • 1
  • 1
Andrew
  • 1,006
  • 10
  • 17
  • Which specific part are you struggling with. This isn't a translation service. The only main syntax difference is the `fsw.Created += file_OnCreated;` should use `AddHandler fsw.Created, file_OnCreated` etc. – Matt Wilko Feb 17 '16 at 15:25
  • 1
    http://stackoverflow.com/q/24578937/1070452 It is possible to learn from questions too – Ňɏssa Pøngjǣrdenlarp Feb 17 '16 at 15:26
  • Sorry @MattWilko - It's not the handlers I'm struggling with, it's the dynamic creation of multiple FileSystemWatchers from an array of folders. Monitoring multiple paths from an unknown number of paths that may be defined at run time. I'm looking for a VB.NET example of how to monitor multiple paths, not how to add handlers. I have working code for monitoring a single folder, or for multiple folders if I have a known fixed number of folders, it's the dynamic creation of multiple monitors from an array or other list of paths that I'm struggling with – Andrew Feb 17 '16 at 15:29
  • 1
    You need to show your attempt and explain specifically what you are having a problem with in order for someone to help you. Without any code all anyone can do is translate the whole answer for you which you can do yourself with a free online converter – Matt Wilko Feb 17 '16 at 15:31
  • @plutonix thanks but that doesn't address the creation of an (until run time) unknown number of paths to monitor. Still useful to read though. – Andrew Feb 17 '16 at 15:42

1 Answers1

0

With prompting from Matt Wilko to push my tired brain a bit further I have found a solution, thanks.

Private fsWatchers As New List(Of FileSystemWatcher)()

Public Sub AddWatcher(wPath As String)
    Dim fsw As New FileSystemWatcher()
    fsw.Path = wPath
    AddHandler fsw.Created, AddressOf logchange
    AddHandler fsw.Changed, AddressOf logchange
    AddHandler fsw.Deleted, AddressOf logchange
    fsWatchers.Add(fsw)
End Sub

With a handler that looks something like:

Public Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    'Handle change, delete and new events
    If e.ChangeType = IO.WatcherChangeTypes.Changed Then
        WriteToLog("File " & e.FullPath.ToString & " has been modified")
    End If

    If e.ChangeType = IO.WatcherChangeTypes.Created Then
        WriteToLog("File " & e.FullPath.ToString & " has been created")
    End If

    If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
        WriteToLog("File " & e.FullPath.ToString & " has been deleted")
    End If
End Sub

Then call AddWatcher with each of your paths to monitor (from array, XML file or form List depending on your needs) and when you've added all paths set your filters and start monitoring:

For Each fsw In fsWatchers
        fsw.NotifyFilter = IO.NotifyFilters.DirectoryName
        fsw.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
        fsw.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
        fsw.EnableRaisingEvents = True
    Next
Andrew
  • 1,006
  • 10
  • 17
  • Sadly this is not a useful answer to anyone else visiting so I am voting to close this question. – Matt Wilko Feb 17 '16 at 16:57
  • @MattWilko ?? It would have been useful to me. There are no other examples anywhere that I've found of how to add multiple file watchers dynamically in VB.NET anywhere, this is an answer to that. Other people like me use this site to look for examples, this is one. – Andrew Feb 17 '16 at 17:18
  • But the essence to the question is about adding multiple objects at runtime of which there are many examples. – Matt Wilko Feb 17 '16 at 17:20