I have build a Windows service which keeps listing the events raised by files on different folder location with a FileSystemWatcher
. I am getting the list of folder location from SQL database in OnStart
method as code of OnStart
is shown below.
public void OnStart()
{
try
{
// get the folder location list from Database
var getListOfFOlderLocation = DbHelper.GetFolderList();
// Get the maximum number of concurrent processes that can be active (if specified)
if (ConfigurationManager.AppSettings["maxConcurrentProcesses"] != null)
maxConcurrentProcesses = Convert.ToUInt32(ConfigurationManager.AppSettings["maxConcurrentProcesses"]);
// Instantiate the regulation semaphore to enforce the maximum process count
executionRegulator = new SemaphoreCounter(maxConcurrentProcesses);
// System.Diagnostics.Debugger.Launch();
// loop though in multiple directories to monitor files
foreach (string sMonitorFolder in DirectoryToWatch())
{
// Create and instantiate an individual FileSystemWatcher for each file set
FileSystemWatcher fileSysetmWatcher = new System.IO.FileSystemWatcher(sMonitorFolder);
if (Directory.Exists(sMonitorFolder))//make sure that mentioned directories exists
{
fileSysetmWatcher.Path = sMonitorFolder;
fileSysetmWatcher.Filter = "*.*";
fileSysetmWatcher.IncludeSubdirectories = true;
fileSysetmWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
fileSysetmWatcher.Created += new FileSystemEventHandler(fileSysetmWatcher_created);
fileSysetmWatcher.EnableRaisingEvents = true;
}
}
new System.Threading.AutoResetEvent(false).WaitOne();
}
catch (Exception ex)
{
Global.WriteLog("Error: " + ex.Message);
}
}
Everything is working fine and I am getting updates if files arrive at particular location.
Problem/Requirements: suppose service is running on a server and now I wanted to add more folders to watch than how do I let know service that more folder list has been updated in this table.
Please let me know if anyone did this before.
As of now after updating the table I am doing start/stop service in order to get updated folder watch list, which is obviously not good option.
I am looking for best way to do this.