1

I am looking to make a Windows service that monitors certain directories using a .NET FileSystemWatcher. My plan is to have one FileSystemWatcher with the IncludeSubdirectores=True. However I do not want to monitor ALL of the sub-directories. So my approach will be to create a "whitelist" of directories that I do actually want to monitor. If a file was changed and it was in a directory that I actually care about I will take some action.

Now here is where my question comes. I am looking to use a Custom Configuration section in an app.config to build my "whitelist".

<ApplicationMonitoring>
    <MainDirectoryMonitored path="C:\MonitoredApps" />
    <SubDirectoriesMonitored>
        <Directory application="App1" path="C:\MonitoredApps\App1" />
        <Directory application="App2" path="C:\MonitoredApps\App2" />
        <Directory application="App3" path="C:\MonitoredApps\App3\SubDir1" />
    </SubDirectoriesMonitored>
</ApplicationMonitoring>

So far so good right? I can set the fileSystemWatcher.Path = to the 'path' attribute of the 'MainDirectoryMonitored' and I can enumerate the 'SubDirectoriesMonitored' and check if it was one that I want to monitor.

What I want to know is if it is possible to validate that the paths of the sub-directories do indeed fall somewhere under the path of the main directory monitored.

Example: Validate that C:\MonitoredApps\App3\SubDir1 does indeed fall somewhere under C:\MonitoredApps etc.

I know it should be pretty intuitive to the user and they should enter stuff correctly but I want to eliminate all errors if possible.

I assume that this could be done with some sort of Custom Configuration Validator but
a) Im not sure if it can use another value from the config to validate against and
b) I dont have a clue on how to do it :)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1018669
  • 91
  • 2
  • 3
  • Please don't prefix your titles with "C#" and such. That's what the tags are for. – John Saunders May 09 '12 at 19:20
  • When this configuration gets updated, you do realize the service is not going to automatically pick up the changes, right? You can setup something to periodically read in the configuration, but given the service you have described, I'm not sure that makes a whole lot of sense. – MattN May 09 '12 at 19:34
  • ConfigurationManager.RefreshSection("ApplicationMonitoring"); Would that not refresh it from the disk? – user1018669 May 09 '12 at 20:09

0 Answers0