-1

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 !

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • a service cannot have a GUI – Franck Nov 05 '18 at 20:31
  • What is your code ? What have you tried ? As pointed out, services don't have GUI, so you're thinking of something else. Formalize clearly what you wan't to do, and what have you done so far. – LoneWanderer Nov 05 '18 at 20:31
  • How comfortable are you with Sockets? You could have your service open a socket on a local port and your client could register and receive broadcasts via a simple protocol. – Ross Bush Nov 05 '18 at 20:52
  • So as I've stated, I realized that Windows Services cannot call a GUI, at least not in a recommended way. And I've also stated clearly that I need to have a directory being watched, and a popup notification thrown when anything in that directory has been changed. I've also mentioned that I have written a windows service that makes use of FileSystemWatcher. But Hitesh has answered my question with a link to an article describing using Task Tray applications. Thank you. – DrunkOldHobo Nov 05 '18 at 21:44

1 Answers1

0

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.

Hitesh Gaur
  • 362
  • 1
  • 11
  • I have already clearly mentioned that I wrote a service making full use of FileSystemWatcher. I am very well acquainted with that. However your link to the article about the Task Tray Applications is a big help. And your other link was extremely interesting as well. Thank you ! – DrunkOldHobo Nov 05 '18 at 21:48