C# 如何同时查看三个文件夹

C# 如何同时查看三个文件夹,c#,visual-studio-2010,visual-studio,file-monitoring,file-watcher,C#,Visual Studio 2010,Visual Studio,File Monitoring,File Watcher,我现在一次监控一个文件夹,我想知道如何同时监控三个文件夹。 请查看下面我的更新代码,,,我想知道我是否应该给出 m_Watcher.Path=draft和m_Watcher.Path=release和m_Watcher.Path=archive这三行与否 我的代码: Dictionary<string, FileSystemWatcher> monitor = new Dictionary<string, FileSystemWatcher>(); p

我现在一次监控一个文件夹,我想知道如何同时监控三个文件夹。

请查看下面我的更新代码,,,我想知道我是否应该给出
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);
    }

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

Dictionary<string,FileSystemWatcher> monitor = new Dictionary<string,FileSystemWatcher>();
现在,对于要添加的每个文件夹,请调用以下方法:

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变量来获取触发事件的文件夹。

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

Dictionary<string,FileSystemWatcher> monitor = new Dictionary<string,FileSystemWatcher>();
现在,对于要添加的每个文件夹,请调用以下方法:

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变量来获取触发事件的文件夹。

创建三个
FileSystemWatcher
实例并观察它们。如果其他文件夹不是主(被观察)文件夹的子文件夹,则需要三个独立的
FileSystemWatcher
,您可以对这三个事件使用相同的事件
OnChanged
。@Habib您的意思是我创建了一个类似
file\u watcher()1
的函数,然后
file\u watcher()2
来监视其他两个folder@stacykeb,而不是多个函数,使用单个函数并传递文件夹名称,(如果要对所有文件夹应用相同的筛选器和通知)。创建
FileSystemWatcher
的三个实例并查看它们。如果其他文件夹不是主文件夹的子文件夹(已查看)文件夹中,则需要三个独立的
文件系统监视程序
,您可以对这三个文件使用相同的事件
OnChanged
。@Habib您的意思是我创建了一个类似
文件监视程序()1
的函数,然后
文件监视程序()2
监视或其他两个folder@stacykeb,而不是多个函数,使用一个函数并传递文件夹名称(如果要对所有文件夹应用相同的筛选器和通知)。我要监视这三个文件夹
tring arch=ini.ReadValue(“位置”,“存档”);string relea=ini.ReadValue(“位置”,“Release”);string draft=ini.ReadValue(“Location”、“draft”);
@stacykeb现在只需使用以下方法:monitorFolder(draft);monitorFolder(Release);monitorFolder(archive);此
monitorFolder
不在我的代码中..其显示错误请告诉我..我的代码中没有名为
monitorFolder
的变量function@stacykeb因此,请将我提供的代码添加到您的代码中并使用iti。我希望监视这三个文件夹
tring arch=ini.ReadValue(“位置”、“存档”);string relea=ini.ReadValue(“位置”、“发布”);string draft=ini.ReadValue(“位置”、“草稿”);
@stacykeb现在只需使用以下方法:monitorFolder(草稿);monitorFolder(发布);monitorFolder(存档);此
monitorFolder
不在我的代码中..其显示错误请告诉我..我的代码中没有名为
monitorFolder
的变量function@stacykeb因此,将我提供的代码添加到您的代码中并使用它