首页 > 解决方案 > Folder Monitor that triggers a Popup

问题描述

I am attempting to create some kind of service that watches a specific directory for changes. When a certain change occurs, there is to be a WPF popup window. This service should also be accessible via the lower right task try in Windows (Windows 10, Visual Studio 2015).

What is the best method for doing this? I've written a Windows Service that makes use of the FileSystemWatcher. But I am unable to trigger any popup notification or GUI from there. There are ways, but none that are recommended practice.

What is a good alternative method for accomplishing my goal?

Thank you !

标签: c#visual-studioservicemonitoringfilesystemwatcher

解决方案


如果您阅读这篇 SO 文章,您将了解一些关于为什么通常不可能在同一个项目中拥有服务和 GUI 应用程序的原因。

正如本文在代码项目中指出的那样,如果您想通过使用托盘图标应用程序来解决它,您可以从 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
}

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


推荐阅读