Howto: Regex & FileSystemWatcher Filter

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
 
L

Lloyd Sheen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top