问题描述
我已经构建了一个Windows服务,该服务始终使用FileSystemWatcher
列出不同文件夹位置上的文件引发的事件。我正在使用OnStart
方法从sql数据库中获取文件夹位置的列表,因为OnStart
的代码如下所示。
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);
}
}
一切正常,如果文件到达特定位置,我会得到更新。
问题/要求:假设服务正在服务器上运行,现在我想添加更多要监视的文件夹,而不是如何让服务知道该表中的更多文件夹列表已被更新。
请让我知道之前是否有人这样做过。
到目前为止,更新表后,我正在执行启动/停止服务以获取更新的文件夹监视列表,这显然不是一个好选择。
我正在寻找做到这一点的最佳方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)