design to overcome filesystemwatcher filter limitation

  • Thread starter Thread starter emailtonitin
  • Start date Start date
E

emailtonitin

I am facing a design issue in my code related to the
filesystemwatcher's filter property.
I figured out that the filter won't accept multiple patterns for eg.
"*.txt, *.csv" don't work.

To workaround this issue, I have 2 options:
1. To create multiple instances of filesystemwatcher class and hook all
of them to the same event handlers.
2. To use 1 instance of filesystemwatcher to listen to all files using
blank filter. And in the event handler sort out the files that I need.
for eg. If (filename.endswith("*.txt") || filename.endswith("*.csv"))
//do stuff

My question is which approach is going to be more efficient, provided:
The list of file extensions that I need to watch are configurable (are
subject to change/number of extensions to be watched may go up)

Want to know your thoughts before deciding eitherway.
Thanks,
Nitin
 
Personally I would use one and a regex to do the filterring but perhaps
someone knows a better way.

Cheers,

Greg
 
Greg said:
Personally I would use one and a regex to do the filterring but perhaps
someone knows a better way.

That's probably the best approach, imho. Multiple watchers means
multiple OS waits: it's definitely going to be more efficient to watch
a whole directory than to watch multiple patterns.

Regex isn't going to be much (if any) slower than looping through an
array of masks and comparing each mask to
Path.GetExtension().ToLower() ... and it will be much smaller code.
 
Thanks guys.
Yes, using multiple watchers didn't seem right to me too.
Ok, I'll go the single watcher + regex way.

As a sidenote, the lack of support for multiple patterns in the filter
property of the filesystemwatcher seems more like an incomplete
functionality of the class, prevelant since framework version 1.1.
Lets hope that some future release takes care of it.
 
Back
Top