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 !
If you go through this SO article, you will understand a little bit about why it is usually not possible to have service and GUI application in same project.
As pointed out in this article at code project, if you want to solve it by using a tray icon application you may follow this article from 2007 and
you may look into using System.IO.FileSystemWatcher fileWatcherService
by setting Path and filter properties according to your need.
fileWatcherService.Path = 'your_folder_path';
fileWatcherService.Filter = "*.*"; //watching all files
fileWatcherService.EnableRaisingEvents = true; //Enable file watcher
when disposing form remember to disable event raising property:
fileWatcherService.EnableRaisingEvents = false;
And, when you need to associate any action, attach event handlers to events like:
public event FileSystemEventHandler Deleted;
public event FileSystemEventHandler Created;
public event FileSystemEventHandler Changed;
public event RenamedEventHandler Renamed;
public event ErrorEventHandler Error;
For example,
fileWatcherService.Created += new System.IO.FileSystemEventHandler(this.fileWatcherService_OnFileCreated);
While,
private void fileWatcherService_OnFileCreated(object sender, FileSystemEventArgs e)
{
//Put your popup action over here
}
You can perform same action for changed, deleted and renamed events by using a reusable common method to invoke popup.