File System Watcher Usage

  • Thread starter Thread starter Stuart Ferguson
  • Start date Start date
S

Stuart Ferguson

I am currently writing a Windows service which monitors multiple folders
and processes the files in them accordingly. The folders to be monitored
are stored in a string array.

I can think of 2 ways of determining which folder has had the file
dropped into it and was wondering if there was a better way. The 2
solutions i have are as follows.

1) When the file is picked up by the OnChanged event search the array to
determine the file type (by matching the path the file is in against a
path in the file profile)

2) Create a different event for each file profile (folder being
monitored) and then process accordingly.

I need the application to be easily scalable thus option 2 isnt the best
for me as code changes would be needed for the new file profile which i
am trying to avoid.

If anyone has any better methods they would be appreciated.

Thanks,

Stuart Ferguson
 
Hi,


On the contrary I think that the solution 2 is the best for you, create a
collection of FileSystemWatchers and point each one to the directory you
want. if all of them use the same event handler to process the OnChanged
events it;s easier, otherwise it gets more complex

like this:

//member variable
ArrayList watchers= new ArrayList();

void CreateWatchers()
{
foreach( string dir in DirectoryArray)
{
FileSystemWatcher watcher = new FileSystemWatcher ();
watcher.Path = dir;
...
watcher.OnChanged = new ....
watchers.Add( watcher);
}
}


Cheers,
 
Back
Top