触发弹出窗口的文件夹监视器

Folder Monitor that triggers a Popup

我正在尝试创建某种服务来监视特定目录的更改。当发生某种变化时,将有一个 WPF 弹出窗口 window。该服务也应该可以通过 Windows (Windows 10, Visual Studio 2015).

中右下角的任务尝试访问。

执行此操作的最佳方法是什么?我已经编写了一个使用 FileSystemWatcher 的 Windows 服务。但我无法从那里触发任何弹出通知或 GUI。有很多方法,但 none 是推荐的做法。

实现我的目标的替代方法是什么?

谢谢!

如果你经历过this SO article,你就会明白为什么通常不可能在同一个项目中同时拥有服务和GUI应用程序。

正如 this article at code project, if you want to solve it by using a tray icon application you may follow this article from 2007 和 您可以根据需要通过设置路径和过滤器属性来考虑使用 System.IO.FileSystemWatcher fileWatcherService

fileWatcherService.Path = 'your_folder_path';
fileWatcherService.Filter = "*.*"; //watching all files
fileWatcherService.EnableRaisingEvents = true; //Enable file watcher

处理表单时记得禁用事件引发属性:

fileWatcherService.EnableRaisingEvents = false;

并且,当您需要关联任何操作时,将事件处理程序附加到事件,例如:

public event FileSystemEventHandler Deleted;
public event FileSystemEventHandler Created;
public event FileSystemEventHandler Changed;
public event RenamedEventHandler Renamed;
public event ErrorEventHandler Error;

例如,

fileWatcherService.Created += new System.IO.FileSystemEventHandler(this.fileWatcherService_OnFileCreated);

同时,

private void fileWatcherService_OnFileCreated(object sender, FileSystemEventArgs e)
{
      //Put your popup action over here
}

您可以通过使用可重复使用的通用方法调用弹出窗口来对已更改、已删除和已重命名的事件执行相同的操作。