【发布时间】:2013-08-06 23:38:42
【问题描述】:
我正在尝试开发一个概念验证的 WCF Web 服务,它使用 FileSystemWatcher 类来监听目录并简单地输出更改的类型和更改的文件的完整路径。
我有一个按预期工作的示例控制台应用程序,但是当我将它移植到 WCF 库中时,文件更改的事件处理程序永远不会触发。
代码:
public void MonitorFolder()
{
System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
watcher.Path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "folder-to-watch");
watcher.IncludeSubdirectories = false;
watcher.Changed += watcher_Changed;
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
watcher.EnableRaisingEvents = true;
}
void watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
Console.WriteLine(string.Format("Change: {0}, File: {1}", e.ChangeType, e.FullPath));
}
在调用服务的客户端类中:
static void Main(string[] args)
{
FileListenerClient c = new FileListenerClient();
c.MonitorFolder();
c.Close();
}
任何想法为什么这不会获取文件更改?
【问题讨论】: