0

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);
  • 8
    Create three instances of `FileSystemWatcher` and watch them. – Sriram Sakthivel Dec 18 '14 at 15:21
  • 1
    If the other folders are not the sub folders of the main *(watched)* folder, then you will need three separate `FileSystemWatchers`, You can use the same event `OnChanged` for all three of them. – Habib Dec 18 '14 at 15:21
  • @Habib so you mean i have create a function like `file_watcher()1` then `file_watcher()2` to monitror other two folder –  Dec 18 '14 at 15:23
  • @stacykeb, instead of multiple functions, have a single function and pass your folder name, *(if you want to apply same filters and notifications for all the folders)*. – Habib Dec 18 '14 at 15:25

1 Answers1

4

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.

MaorB
  • 117
  • 2
  • 10
  • i want to monitor these three folder `tring arch= ini.ReadValue("Location", "Archive"); string relea = ini.ReadValue("Location", "Release"); string draft = ini.ReadValue("Location", "Draft");` –  Dec 18 '14 at 15:50
  • 2
    @stacykeb Now just use the method: monitorFolder(draft); monitorFolder(release); monitorFolder(archive); – MaorB Dec 18 '14 at 16:06
  • this `monitorFolder` is not in my codes.. its showing error –  Dec 18 '14 at 16:14
  • please let me know.. i dont have a variable called `monitorFolder` in my function –  Dec 18 '14 at 16:24
  • 1
    @stacykeb So Add the code I supplied to your code and use it – MaorB Dec 18 '14 at 16:25
  • Ok one last question.. what i should do to this line `m_Watcher.Path = draft;` because its only watcing draft now.. no other folders are watching –  Dec 18 '14 at 16:39
  • its only watching draft location –  Dec 18 '14 at 17:04
  • i got it.. please see my updated codes.. that how i used. and worked.. perfectly.. thanks for helping me. `m_Watcher.Path = folderPath;` this is the line i was looking for!! –  Dec 18 '14 at 17:42