Howto: Regex & FileSystemWatcher Filter

  • Thread starter Thread starter Shmulik
  • Start date Start date
S

Shmulik

How can I (can I?) use a regular expression as the filter to a
FileSystemWatcher?

I want to watch for something like this:

Regex regex = new Regex(@"^Current(One|Two|Three)File\.txt$",
RegexOptions.IgnoreCase);

That is, is the file CurrentOneFile.txt, CurrentTwoFile.txt, etc. shows in
in the directory I'm watching, then I can take action.

I can't assign the regex to the filter though:

FileSystemWatcher fsw = new FileSystemWatcher ();
fsw.Filter = regex; // won't work


How can I accomplish this?

TIA
 
If you read the docs it accepts only a string. You will have to get all
watch for all *.txt and then apply the regex to any file changes you are
notified of. For what you are doing a regex might be a little heavy for the
file name comparison.

Lloyd Sheen
 
Back
Top