繁体   English   中英

创建同一FileSystemWatcher的多个实例

[英]Create multiple instances of the same FileSystemWatcher

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

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

尝试这里的“工厂方法模式”建议:我得到以下错误:“'列表'不包含'添加'的定义

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

使用工厂方法模式

    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)
        {
            watcher.add(MyWatcherFatory(ArrayPaths[i]));
            i++;
        }
        //Do other stuff....
        //....
        //Start my watchers...
        foreach (FileSystemWatcher watcherin 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);
        }          
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM