FileSystemWatcher Class.. Filters?

  • Thread starter Thread starter Ryan Rogers
  • Start date Start date
R

Ryan Rogers

The FileSystemWatcher Class has a filter property where you can tell it what
files you want it to watch for. I need it to watch for *.p** and *.f**
files both and have tried giving the filter the following strings
"*.p**;*.f**" and "*.p**,*.f**" and Each one on its own. Only when I supply
each one on its own does it pick up anything. My way around this was to
just use an if statement in the event triggers to look at the filename there
and drop it if its not what I want. I'm wondering if there is a better way?
Do I have the filter String Syntax wrong?

Ryan Rogers
 
I think your main problem is your filter string is incorrect and you need to
create multiple FileSystemWatcher objects to achieve your goal. Here is
some sample code.

watcher1 = new FileSystemWatcher(Application.StartupPath);

watcher2 = new FileSystemWatcher(Application.StartupPath);

watcher1.Filter = "*.p*";

watcher2.Filter = "*.f*";

watcher1.EnableRaisingEvents = true;

watcher2.EnableRaisingEvents = true;

watcher1.Created += new FileSystemEventHandler(this.HandleFileCreatedEvent);

watcher2.Created += new FileSystemEventHandler(this.HandleFileCreatedEvent);

watcher1.Deleted += new FileSystemEventHandler(this.HandleFileDeletedEvent);

watcher2.Deleted += new FileSystemEventHandler(this.HandleFileDeletedEvent);


--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
That was another idea of mine however I have four servers to watch.. so that
means I have to create 8 fileSystemwatchers. Seems kind of silly when I can
just create four and use and If statement in my Event that triggers on file
creation which it does now. II was more just curious if One
FileSystemWatcher object can handle the filters. Thanks for the advice
though.

Ryan

Jared Parsons said:
I think your main problem is your filter string is incorrect and you need to
create multiple FileSystemWatcher objects to achieve your goal. Here is
some sample code.

watcher1 = new FileSystemWatcher(Application.StartupPath);

watcher2 = new FileSystemWatcher(Application.StartupPath);

watcher1.Filter = "*.p*";

watcher2.Filter = "*.f*";

watcher1.EnableRaisingEvents = true;

watcher2.EnableRaisingEvents = true;

watcher1.Created += new FileSystemEventHandler(this.HandleFileCreatedEvent);

watcher2.Created += new FileSystemEventHandler(this.HandleFileCreatedEvent);

watcher1.Deleted += new FileSystemEventHandler(this.HandleFileDeletedEvent);

watcher2.Deleted += new FileSystemEventHandler(this.HandleFileDeletedEvent);


--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"

Ryan Rogers said:
The FileSystemWatcher Class has a filter property where you can tell it what
files you want it to watch for. I need it to watch for *.p** and *.f**
files both and have tried giving the filter the following strings
"*.p**;*.f**" and "*.p**,*.f**" and Each one on its own. Only when I supply
each one on its own does it pick up anything. My way around this was to
just use an if statement in the event triggers to look at the filename there
and drop it if its not what I want. I'm wondering if there is a better way?
Do I have the filter String Syntax wrong?

Ryan Rogers
 
Back
Top