Click here to Skip to main content
12,696,092 members (29,163 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

36.3K views
29 bookmarked
Posted

Using FileSystemWatcher to monitor multiple directories

, 20 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
There are times that we need to monitor multiple directories and if any changes are available, invoke a given method.

In a previous example, I have showed how to use the FileSystemWatcher class to monitor a directory. But there are times that 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.

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

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):

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

License

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

Share

About the Author

Manjuke Fernando
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My current role involves architecting and building a variety of data solutions, providing database maintenance and administration support, building the organization’s data practice, and training and mentoring peers.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science.


Specialities: SQL Server, T-SQL Development, SQL Server Administration, SSRS, SSIS, C#, ASP.Net, Crystal Reports

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionService version? Pin
Dan Oviatt26-Jun-12 11:28
memberDan Oviatt26-Jun-12 11:28 
AnswerRe: Service version? Pin
Manjuke Fernando26-Jun-12 17:19
memberManjuke Fernando26-Jun-12 17:19 
GeneralRe: Service version? Pin
Dan Oviatt27-Jun-12 4:19
memberDan Oviatt27-Jun-12 4:19 
GeneralRe: Service version? Pin
justmicros13-Nov-12 10:57
memberjustmicros13-Nov-12 10:57 
GeneralRe: Service version? Pin
Manjuke Fernando13-Nov-12 14:33
memberManjuke Fernando13-Nov-12 14:33 
Questionvery interesting artice Pin
mike231521-Oct-11 3:32
membermike231521-Oct-11 3:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 20 Oct 2011
Article Copyright 2011 by Manjuke Fernando
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid