Dynamically create filewatchers based on entries in the app.config file for a windows service

Posted on 2007-07-19
Last Modified: 2012-05-05
I've created a windows service whose purpose is to move files from the local disk to shared network drives.  I've got the basics working on the dev machine but I'm looking to expand the functionality and have hit a stumbling block.  At the start this service should be able to read the app.config file and for each setting that it finds it should create a new filewatcher.  The value of the setting key will contain the origin folder and the target location.   It will set the new filewatcher properties and then add each watcher to an array.  What I'm struggling with is how to code for the FileSystemWatcher_Created event of each watcher in the array, which is where the actual file moves will take place.  Here's what I have so far.  Can someone help me get to the next steps?

Dim mycount As Integer = System.Configuration.ConfigurationSettings.AppSettings.Count
        Dim x As Integer = 0
        Dim mysetting As String = ""
        Dim myarray(0) As System.IO.FileSystemWatcher

        For x = 0 To mycount - 1
            mysetting = System.Configuration.ConfigurationSettings.AppSettings(x)
           ' the first setting is a TESTPARM that should be skipped
            If mysetting = "Y" Or mysetting = "P" Or mysetting = "T" Or mysetting = "" Or _
             IsNothing(mysetting) Or IsDBNull(mysetting) Then
                ' do nothing
                Dim mytext() As String = Split(mysetting, ";")
                Dim origin As String = mytext(0)
                Dim target As String = mytext(1)
                Dim mywatcher As New FileSystemWatcher
                mywatcher.Path = origin
                mywatcher.IncludeSubdirectories = False
                mywatcher.NotifyFilter = NotifyFilters.FileName
                mywatcher.EnableRaisingEvents = True
                ReDim Preserve myarray(x)
                myarray(x) = mywatcher
            End If

Question by:bozworthy
    1 Comment
    LVL 46

    Accepted Solution

    Hi bozworthy,

    Use something like this....

                    myarray(x) = mywatcher
                    AddHandler mywatcher.Created, AddressOf FileSystemWatcher_Created

    ...then this sub to handle the created event for each....

        Private Sub FileSystemWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
            Dim mywatcher As FileSystemWatcher = CType(sender, FileSystemWatcher)
        End Sub



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now