I have a FileSystemWatcher that watches a directory. This has 3 sub-directories. I was thinking of just making 3 different FileSystemWatcher objects to watch each directory individualy, because when a file is created in one of these directories I need different events raised. For directory A, I want MethodA called, and dir B I need MethodB called and Method C called when a new file is placed in Directory C. Can one FileSystemWatcher call different methods depending on the path of the created file I use this :
watcher.Created +=
new FileSystemEventHandler(FileCreated);
to call my FileCreated() method, any ideas Thanks...
Can FileSystemWatcher raise different events depending on file path?
Ikuko O.
Yeshia,
If you want to use a single FileSystemWatcher to call different methods depending on the sub-folder you will have to do this yourself. It's not too difficult since the various events will provide a FileSystemEventArgs parameter containing the FullPath property. You can extract the directory simply using Path.GetDirectoryName, and then apply your switch logic. For instance, if your folders are known statically, you could write something like:
private void FileWatcher_Created (object sender, FileSystemEventArgs e) {
switch (Path.GetDirectoryName (e.FullPath)) {
case @"c:\myfolder\subA":
methodA ();
break:
case @"c:\myfolder\subB":
methorB ();
break;
...
}
}
HTH
--mc