我有一个在启动时打印报告的 Windows 服务。我有两种情况-
1.我想在我的 E:\here\文件夹中创建了一个新文件夹后启动此服务。如何完成此操作?
2 是否也可以在数据库表中插入新行时触发服务?如何?
请提供上述查询的链接。
最佳答案
您可以使用 FileSystemWatcher检查是否在特定位置创建了特定文件夹。创建后,您可以启动 Windows 服务。
创建 FileSystemWatcher
//Define this in the clas level
FileSystemWatcher watcher = new FileSystemWatcher();
正在初始化
private void InitializeWatcher()
{
watcher.Created += new FileSystemEventHandler(NotifyChange);
watcher.Path = @"E:\here\folder";
watcher.EnableRaisingEvents = true;
}
创建文件夹时启动 Windows 服务
private void NotifyChange(object sender, FileSystemEventArgs e)
{
if (e.Name.Equals("FolerName",StringComparison.OrdinalIgnoreCase))
{
new ServiceController("WindowsServiceName").Start();
}
}
关于c# - 如何在创建新文件夹或文件时触发 Windows 服务启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812938/