Using FileSystemWatcher to synchronize ComboBox options

In this post I will present you with my latest addition to my personal user experience arsenal: direct file system synchronization. You will be shown how easily it is for you to setup a FileSystemWatcher to observe file changes on disc, astonishing your user when he least expects.


Download sample source code

You can download a running sample for this blog post by following this link.



Suppose you have a report templates folder under your application documents folder containing a series of report formats that you want your user to be able to add, edit or remove through the file system.


In that folder you store Report1.rpt, Report2.rpt and Report3.rpt. Your application has a dialog that enables the user to select which report template he should use to print data. He is running the application and notices that the template he wants to use is not on the application’s template folder. He goes to the email that his boss had sent him, and gets the NewReport.rpt file. He copies the file to your Software’s template folder.


The Problem

At this point the user has the report on his folder but the combo box on the UI he is using has not update. There is no way for him to select the new template without reopening the UI, or maybe the application (if you are caching the report templates list). This may force him to lose some work.

A normal approach in this scenario is to place a refresh button in the UI that may be used to refresh the templates list. Although this fixes the problem, it forces the user to an additional step.


Let FileSystemWatcher do its magic

A much clear solution is to use the FileSystemWatcher class that is part of System.IO and it has lived there for a long time now! You would create a method that updates the report templates from disk. Then create a new FileSystemWatcher that monitors *.rpt files that change on the templates folder.

var watcher = new FileSystemWatcher(TemplatesPath, TemplatesSearchPattern);
watcher.Created += (fileSystemWatcher_FilesChanged);
watcher.Changed += (fileSystemWatcher_FilesChanged);
watcher.Deleted += (fileSystemWatcher_FilesChanged);
watcher.Renamed += (fileSystemWatcher_FilesChanged);
watcher.EnableRaisingEvents = true;

Has you can see you can monitor specific events only. In this case we want to monitor every change that might occur in our report templates folder that respects to rpt files. Now whenever a change is made on the file system the UpdateAvailableTemplates method will be called and we can synchronize the report templates list.

The only additional catch is to guarantee that UpdateAvailableTemplates runs on the thread it is supposed to run. We do this by saving the SynchronizationContext in which our FileSystemWatcher is created.

this.syncContext = SynchronizationContext.Current;

Then when a change is detected we post the UpdateAvailableTemplates to the context we saved on the constructor:

private void fileSystemWatcher_FilesChanged(object sender, FileSystemEventArgs e)
    this.syncContext.Post(UpdateAvailableTemplates, null);

To test your application go ahead and delete, rename, create some files and see how your application keeps on updating the list it displays. Notice that it is instantaneous.

You can download a running sample for this blog post by following this link.



Obviously that the templates list will not change that often, you may be wondering if this extra mile is worth the effort. Either way you will have to implement the UpdateAvailableTemplates logic. At the cost of these extra lines of code you can add that extra touch of excellence to your user’s experience.

Most of your users will not require this feature but when they actually need it, it will probably save them a lot of effort. For the effort it takes to implement such thing, if you save 1 costumer the endeavor of filling that form again it will be worth it.

When I added this to the software I am currently working on, I had a few WOW’s, when people saw that the UI just worked. After several deletes, adds, edits, etc, on the files the software updated in real time!

Shout it kick it on

One thought on “Using FileSystemWatcher to synchronize ComboBox options

  1. Pingback: DotNetShoutout

Leave a Reply

Your email address will not be published. Required fields are marked *