Skip to content

FileSystemwatcher Filter

alex

Asked Mar 3, 2006, 2:44 AM EST2 answers
Share
LinkedInX
I'm busy creating a app to monitor files on the HDD. My question is how do i specify multiple filter types. i.e. for the system to look out for all .txt and all .xml files ?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The answer is - you can't. At least not with a single watcher. The filter is a single pattern matcher, not a list. It's easier to do your own filtering of events as they are generated.

    Anthony
    0
  • 0

    Rightly so, you probably want to perform a custom filter inside the appropriate handler and ignore completely the

    FileSystemWatcher.Filter method:

    FileSystemWatcher watcher = new FileSystemWatcher();

    watcher.Path = @"C:\";
    watcher.Created += new FileSystemEventHandler(OnCreated);
    watcher.EnableRaisingEvents = true;

    private static void OnCreated(object source, FileSystemEventArgs e)
    {
     if (e.Name.Substring(e.Name.LastIndexOf('.')) == ".xml")
     {
      ...
     }

     if (e.Name.Substring(e.Name.LastIndexOf('.')) == ".txt")
     {
      ...
     }
    ...
    }

    0