创建同一个 FileSystemWatcher 的多个实例

Create multiple instances of the same FileSystemWatcher

我的程序需要监控多个位置,但为每个位置触发相同的代码。由于单个 FileSystemWatcher 无法监视多个位置,但是否可以创建它的多个实例并为每个实例传递一个文件夹路径?

我无法对每个 FileSystemWatcher 进行硬编码,因为需要及时添加越来越多的位置,这需要由最终用户完成,因为这对我来说非常不切实际每次手动硬编码一个新的FileSystemWatcher。所以我的计划是将文件夹路径保存到一个文件中,程序只是为列表中的每个路径创建一个 FileSystemWatcher。但我不知道这是否有可能。

这里继续工厂方法模式的建议尝试: 我收到错误:“'List' 不包含 '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);
        }          
    }

使用 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.
    }

编辑:根据您进一步提供的信息:

    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);
        }          
    }