Multiple Folder Watcher

In application development you can come across to scenario where you need to keep an eye on a particular folder and do some processing based on if file is added, deleted, changed or renamed in that folder.
To achieve the same the best available option is to create the Windows Service Created for the same.

But in case if you have multiple folders to watch from the same Windows Service than you need to use multiple instance of the FileSystemWatcher() class.

Here is the sample code to watch the multiple folders:
/************************************************************************/
// TODO: Add code here to start your service.
_watcher = new FileSystemWatcher();
_watcher.Path = ConfigurationManager.AppSettings["ScanDocumentSharedPath"].ToString();
_watcher.Filter = "*.tif";
//_watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
_watcher.Created += new FileSystemEventHandler(LogFileSystemCreated);
//_watcher.Deleted += new FileSystemEventHandler(LogFileSystemDeleted);
_watcher.Renamed += new RenamedEventHandler(LogFileSystemRenaming);
//_watcher.Error += new ErrorEventHandler(LogBufferError);
_watcher.EnableRaisingEvents = true;

_watcher1 = new FileSystemWatcher();
_watcher1.Path = ConfigurationManager.AppSettings["ScanDocumentSharedPath1"].ToString();
_watcher1.Filter = "*.tif";
//_watcher.Changed += new FileSystemEventHandler(LogFileSystemChanges);
_watcher1.Created += new FileSystemEventHandler(LogFileSystemCreated);
//_watcher.Deleted += new FileSystemEventHandler(LogFileSystemDeleted);
_watcher1.Renamed += new RenamedEventHandler(LogFileSystemRenaming);
//_watcher.Error += new ErrorEventHandler(LogBufferError);
_watcher1.EnableRaisingEvents = true;
//UploadLeftOverFiles();
textBox1.Text = "Document Folder Monitoring Started";

void LogFileSystemCreated(object sender, FileSystemEventArgs e)
{
//Perform you operation from this file created event
textBox1.Text = log + string.Format("Created {0} | {1}", e.FullPath, e.ChangeType);
}

void LogFileSystemRenaming(object sender, RenamedEventArgs e)
{
//Perform you operation from this file Renaming event
}

/************************************************************************/

Comments

Popular posts from this blog

WCF WSDL location address Issue resolved when hosted over HTTPS with basicHTTPBinding

Yellow Background issue in Word to PDF conversion

Gmail tricks - create unlimited siblings of your Gmail address