How to filewatch three folder at same time

243 views Asked by At

I am now monitoring one folder at a time, and i am wondering how can i file watch three folders at same time.

please see my updated codes below,,, i am wondering wether i should give m_Watcher.Path = draft; and m_Watcher.Path = release; and m_Watcher.Path = archive; these three lines or not

My codes:

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

and called the monitorFolder in my function

            monitorFolder(draft);
            monitorFolder(release);
            monitorFolder(archive);
1

There are 1 answers

8
MaorB On BEST ANSWER

Example of monitoring multiple folders: First create some container to hold every FileSystemWatcher object:

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

Then for every folder to add, add it to the container:

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
}

Now, for every folder you would like to add, call the method:

monitorFolder(theDesiredFolderToMonitorPath);

For example to the code above:

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

Then it is possible to get which folder fired the event by working with the EventArgs variable in the event.