Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Using FileSystemWatcher to Monitor Multiple Directories

4.71/5 (6 votes)
20 Oct 2011CPOL 58.6K  
There are times when we need to monitor multiple directories and if any changes are available, invoke a given method.

In a previous example, I showed how to use the FileSystemWatcher class to monitor a directory. But there are times when we need to monitor multiple directories and if any changes are available, invoke a given method.

We can do that by using the following method. First create a class. We’ll call this class Watcher.

C#
 1: public class Watcher
 2:     {
 3:         public string Directory { get; set; }
 4:         public string  Filter { get; set; }   6:
 5:
 6:         private Delegate _changeMethod;
 7:
 8:         public Delegate ChangeMethod
 9:         {
10:             get { return _changeMethod; }
11:             set { _changeMethod = value; }
12:         }
13:
14:         FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
15:
16:         public Watcher(string directory, string filter, Delegate invokeMethod)
17:         {
18:             this._changeMethod = invokeMethod;
19:             this.Directory = directory;
20:             this.Filter = filter;
21:         }
22:
23:         public void StartWatch()
24:         {
25:             fileSystemWatcher.Filter = this.Filter;
26:             fileSystemWatcher.Path = this.Directory;
27:             fileSystemWatcher.EnableRaisingEvents = true;
28:
29:             fileSystemWatcher.Changed +=
                  new FileSystemEventHandler(fileSystemWatcher_Changed);
30:         }
31:
32:         void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
33:         {
34:             if (_changeMethod != null)
35:             {
36:                 _changeMethod.DynamicInvoke(sender, e);
37:             }
38:         }
39:     }

And we can use it to monitor multiple directories as shown below (for this example, I have used a console application and I am only considering the change event):

C#
 1: class Program
 2:     {
 3:         delegate void invokeMethodDelegate(object sender, FileSystemEventArgs e);
 4:
 5:         static void Main(string[] args)
 6:         {
 7:             invokeMethodDelegate mymethod = new invokeMethodDelegate(InvokeMethod);
 8:             Watcher w1 = new Watcher(@"C:\Directory1", "*.*", mymethod);
 9:             w1.StartWatch();
10:
11:             Watcher w2 = new Watcher(@"D:\Directory2", "*.*", mymethod);
12:             w2.StartWatch();
13:
14:             string zRetVal = Console.ReadLine();
15:         }
16:
17:         static  void InvokeMethod(object sender, FileSystemEventArgs e)
18:         {
19:             Console.WriteLine("Change in file {0}", e.FullPath);
20:         }
21:     }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)