c# - 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 !
解决方案
如果您阅读这篇 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
}
您可以通过使用可重用的通用方法调用弹出窗口来对更改、删除和重命名的事件执行相同的操作。
推荐阅读
- amazon-web-services - Apollo Express CORS 选项
- css - 在 css 中使用渐变的形状
- c# - 绑定一个嵌套在网格视图中的网格视图行
- python - pydrake中关于CompassGait的一些问题
- algorithm - Big-Oh 符号并找到合适的 c 和 n0
- ios - AutoFill 又名 Flutter 中的自动登录
- c++ - 更新到 2020 年 5 月版本后无法在 Visual Studio 2019 中创建 .exe 文件
- python - TypeError:“numpy.ndarray”和“list”的实例之间不支持“>”
- c# - InsertAsync Xamarin 提供的数据类型无效
- html - 如何使用css将标题设置为不透明?