Creating a service to monitor a folder structure

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to create a service (or something else) to monitor the coming and
goings of files (& possible folders) beneath a root folder. This service
need to know the path/FileName of any new files so it can add data to a
database. If files are deleted the service needs to know the path/FileName
too for it can delete data from the database. The service will (or execute
another application that will) query inside the added documents for other
data to be added to the database.

I would be greatful for any pointers.
 
Hi MAB,

I suggest creating a Windows Service that uses a
System.IO.FileSystemWatcher. The FileSystemWatcher class exposes events for
operations that you require such as adding a new file and deleting a file.
View MSDN help on FileSystemWatcher for more information and examples.

If you want to buffer changes and handle them in groups just use a
System.Threading.Timer and a buffer.

1. Enable the timer using the Timer.Change method when a FileSystemWatcher
event is raised but only if the timer is not already running. Checking the
Timer state will require a global variable that you must set and reset at
the appropriate times.
2. Add the ChangeType supplied by the FileSystemEventArgs in your event
handler to a System.Collections.Generics.Dictionary<System.IO.FileInfo,
System.IO.WatcherChangeTypes> or a List, Typed with a custom object, which
is probably the better choice.
3. In the Timer event handler reset the global "timer started" variable and
perform the batch operation.
4. You'll probably want to synchronize the code in your TimerCallback with
your FileSystemWatcher event handlers to ensure that no changes go
unbuffered. You can do this using WaitHandles.

HTH
 
Back
Top