如何同时监视三个文件夹

本文关键字:三个 文件夹 何同时 监视 | 更新日期: 2023-09-27 18:34:40

我现在一次监视一个文件夹,我想知道如何同时提交监视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);

如何同时监视三个文件夹

监视多个文件夹的示例:首先创建一些容器来保存每个 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 变量来获取触发事件的文件夹。