如何同时文件夹三个文件夹

时间:2014-12-18 15:19:34

标签: c# visual-studio-2010 visual-studio file-monitoring file-watcher

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

1 个答案:

答案 0 :(得分:3)

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