0

我现在一次监视一个文件夹,我想知道如何同时进行文件监视three folders

请在下面查看我的更新代码,我想知道我是否应该 给出m_Watcher.Path = draft;这 三行m_Watcher.Path = release;m_Watcher.Path = archive;

我的代码:

      Dictionary<string, FileSystemWatcher> monitor = new Dictionary<string, FileSystemWatcher>();
    public void monitorFolder(string folderPath)
    {
        string draft = ini.ReadValue("Location", "Draft");
        string release = ini.ReadValue("Location", "Release");
        string archive = ini.ReadValue("Location", "Archive");
       // System.IO.Directory.CreateDirectory(draft); // no need to check if exists
        if (monitor.ContainsKey(draft)) return; //if directory already being monitored
        if (monitor.ContainsKey(release)) return;
       if (monitor.ContainsKey(archive)) return;
        m_Watcher = new System.IO.FileSystemWatcher();
        m_Watcher.Filter = "*.*";
        m_Watcher.Path = folderPath;  //give the folderpath
        m_Watcher.IncludeSubdirectories = true;
        m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
        m_Watcher.Created += new FileSystemEventHandler(OnChanged);
        m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
        m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
        m_Watcher.EnableRaisingEvents = true;
        ///Initializing delegate that we have created to update UI control outside the current thread
        addItemInList = new delAddItem(this.AddString);
    }

monitorFolder在我的函数中调用

            monitorFolder(draft);
            monitorFolder(release);
            monitorFolder(archive);
4

1 回答 1

4

监控多个文件夹的示例:首先创建一些容器来保存每个 FileSystemWatcher 对象:

Dictionary<string,FileSystemWatcher> monitor = new Dictionary<string,FileSystemWatcher>();

然后对于要添加的每个文件夹,将其添加到容器中:

public void monitorFolder(string folderPath)
{
    System.IO.Directory.CreateDirectory(draft); // no need to check if exists
    if (monitor.ContainsKey( folderPath )) return; //if directory already being monitored
    FileSystemWatcher m_Watcher = new System.IO.FileSystemWatcher();
    m_Watcher.Filter = "*.*";
    m_Watcher.Path = folderPath; 
    m_Watcher.IncludeSubdirectories = true;
    m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    m_Watcher.Changed += new FileSystemEventHandler(OnChanged);
    m_Watcher.Created += new FileSystemEventHandler(OnChanged);
    m_Watcher.Deleted += new FileSystemEventHandler(OnChanged);
    m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);
    m_Watcher.EnableRaisingEvents = true;
    monitor.Add( folderPath,m_Watcher); // add to monitor Container
}

现在,对于您要添加的每个文件夹,调用该方法:

monitorFolder(theDesiredFolderToMonitorPath);

例如上面的代码:

 public void file_watcher()
 {
    string draft = ini.ReadValue("Location", "Draft");
    string release = ini.ReadValue("Location", "Release");//second folder
    string archive = ini.ReadValue("Location", "Archive");
    monitorFolder(draft);
    monitorFolder(release);
    monitorFolder(archive);
 }

然后可以通过使用事件中的 EventArgs 变量来获取哪个文件夹触发了事件。

于 2014-12-18T15:39:50.713 回答