0

In the past I have created Windows Services which monitor one directory where the path is configurable by referencing a config file like this:

fileSystemWatcher1.Path = ConfigurationManager.AppSettings["WatchPath1"];

I've also watched multiple configurable paths by defining multiple fileSystemWatcher methods.

        fileSystemWatcher1.Path = ConfigurationManager.AppSettings["WatchPath1"];
        fileSystemWatcher2.Path = ConfigurationManager.AppSettings["WatchPath2"];
        fileSystemWatcher3.Path = ConfigurationManager.AppSettings["WatchPath3"];

The above works if I know before hand how many folders I am likely going to monitor so my question is, what approach can I take to make this dynamic when I don't know how many folders need to be monitored? What I'd like to do is continue to use a config or XML file and for each entry create FileSystemWatcher with the path specified in the file.

I would also need to be able to dynamically create the a method for each FileSystemWatcher so specific actions can be taken when a file system event is triggered.

Example code to created dynamically:

private void fileSystemWatcher1_Created(object sender,  System.IO.FileSystemEventArgs e)
            {
                Library.WriteErrorLog("New file detected in watch folder.  File: " + e.FullPath);
                // Do stuff with file
            }

Is this possible? If so how could I go about achieving this?

totalfreakingnoob
  • 413
  • 1
  • 9
  • 25

1 Answers1

1

Store a list of FileSystemWatcher object, which you initialise when you start the class.

List<FileSystemWatcher> fsWatchers = new List<FileSystemWatcher>();

To add a new watcher...

public void AddWatcher(String wPath) {
    FileSystemWatcher fsw = new FileSystemWatcher();
    fsw.Path = wPath;
    fsw.Created += file_OnCreated;
    fsw.Changed += file_OnChanged;
    fsw.Deleted += file_OnDeleted;
    fsWatchers.Add(fsw);
}

private void file_OnDeleted(object sender, FileSystemEventArgs e) {

}

private void file_OnChanged(object sender, FileSystemEventArgs e) {

}

private void file_OnCreated(object sender, FileSystemEventArgs e) {

}

In each of the event handlers, cast sender to a FileSystemWatcher if you need to interact directly with it. To get the full path, use the get methods on the event args object (e).

You could potentially simplify it a little by having a single event handler assigned to all the events on the FileSystemWatcher.

private void file_OnFileEvent(object sender, FileSystemEventArgs e) {
    String path = e.FullPath;
    if (e.ChangeType == WatcherChangeTypes.Changed) {

    } else if (e.ChangeType == WatcherChangeTypes.Created) {

    }
}
Alex
  • 1,643
  • 1
  • 14
  • 32
  • is there a way to check the user , or user changes? – Michael Rudner Evanchik Jun 12 '15 at 16:36
  • When you say 'Check the user', I presume you mean check which user changed the file? If so, have a look at this thread. This problem has come up before, but it looks like it's a tricky one to solve. http://stackoverflow.com/questions/11660235/find-out-usernamewho-modified-file-in-c-sharp – Alex Jun 15 '15 at 15:42
  • look like its there thanks will play around if i ever get to that project – Michael Rudner Evanchik Jun 15 '15 at 16:29
  • How would you go about handling configurable destinations too? Like for example you have a config file with 3 paths to monitor and when a file is dropped they get moved to another folder which is specified in the config file. How would you suggest dealing with it? – totalfreakingnoob Jun 16 '15 at 15:25
  • That activity would happen in the event handler. For that, you would need to define what rules govern which file types go to where. I would personally use a Dictionary with the key being a particular file extension, and the value being the path it should be moved to. You could feasibly use a path as the key, and one as the value. If you wanted all files dropped into c:\infiles to be moved to c:\outfiles, you would store those 2 paths in your dictionary. When an OnFile event is triggered, check if you have a path in the dictionary as the key, and move the file to the value path. – Alex Jun 16 '15 at 18:14