2

My program needs to monitor multiple locations, but trigger the same code for each location. As a single FileSystemWatcher can't monitor multiple locations, but is it possible to create multiple instances of it and pass in a folder path for each?

I can't hard code each FileSystemWatcher as more and more locations will need to be added in time and this needs to be done by the end users, as it is highly impractical for me to have to manually hard code a new FileSystemWatcher each time. So my plan was to have the folder paths saved to a file and the program just creates a FileSystemWatcher for each path in the list. But I have no idea if this is possible in the slightest.

Going on the Factory Method Pattern suggestion here the attempt: I get the errors: "'List' does not contain a definition for 'add'

public void StartWatchers()
    {
        string[] ArrayPaths = new string[2];
        List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
        ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
        ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";

        int i = 0;
        foreach (String String in ArrayPaths)
        {
            watcher.add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }
        //Do other stuff....
        //....
        //Start my watchers...
        foreach (FileSystemWatcher watcher in watchers)
        {
            Watcher.EnableRaisingEvents = true;
            i++;
        }

    }

    FileSystemWatcher MyWatcherFatory(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += Watcher_Created;
        watcher.Path = path;
        watcher.Filter = "*.csv";
        return watcher;
    }

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        FileInfo fileInfo = new FileInfo(e.FullPath);
        if (!IsFileLocked(fileInfo))
        {
            CheckNumberOfRecordsInFile(e.FullPath);
        }          
    }
Vereonix
  • 1,341
  • 5
  • 27
  • 54

1 Answers1

6

Use the factory method pattern.

    FileSystemWatcher MyWatcherFatory(string path, object additionalParameters)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += myWatcherChangedMethod;//Attach them to the same listeners,
        //Set additional parameters...
        return watcher.
    }

EDIT: Based on the information you further provided:

    public void StartWatchers()
    {
        string[] ArrayPaths = new string[2];
        List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
        ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
        ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";

        int i = 0;
        foreach (String String in ArrayPaths)
        {
            watchers.Add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }
        //Do other stuff....
        //....
        //Start my watchers...
        foreach (FileSystemWatcher watcher in watchers )
        {
            watcher.EnableRaisingEvents = true;;
            i++;
        }
    }

    FileSystemWatcher MyWatcherFatory(string path)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(path);
        watcher.Changed += Watcher_Created;
        watcher.Path = path;
        watcher.Filter = "*.csv";
        return watcher;
    }

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        FileInfo fileInfo = new FileInfo(e.FullPath);
        if (!IsFileLocked(fileInfo))
        {
            CheckNumberOfRecordsInFile(e.FullPath);
        }          
    }
DGaleano
  • 137
  • 9
srandppl
  • 571
  • 4
  • 14
  • How do I start the returned watcher? I've updated my question to show what I've tried implementing, but it doesn't do anything. I'm finding it hard to get my head around factory method pattern in regards to my specific need, as no examples are of starting multiple instances of the same .net class. – Vereonix Dec 28 '15 at 12:59
  • Ah I understand it now going on your updated code, however ".add" is not supported for 'FileSystemWatcher', I don't know if this means an end to the possibility, or if there is a typo/missing system etc. – Vereonix Dec 28 '15 at 20:17
  • **"'List' does not contain a definition for 'add' – Vereonix Dec 28 '15 at 20:36
  • The method is called "Add". – srandppl Dec 28 '15 at 21:21