C# – Creating a Service to Monitor a Directory
Let’s say you wanted to watch a directory and perform an action each time a file is added, deleted, changed, or renamed.
- Microsoft has a Visual C# Windows Service project template in Visual Studio.
- Microsoft also has a nice class already created to help with this: FileSystemWatcher
Example Project
Here is an example project you can download: DirectoryMonitoring.zip
Step 1 – Create a Visual C# Windows Service project in Visual Studio
- Select File | New Project.
- Select Templates | VIsual C# | Windows | Windows Service.
- Provide a name: DirectoryMonitoring
- Click OK.
Step 2 – Create an object that inherits from FileSystemWatcher
- Right-click on Project and choose Add | Class.
- Name it MyFileSystemWatcher.cs.
using System;
using System.IO;
namespace DirectoryMonitoring
{
public class MyFileSystemWatcher : FileSystemWatcher
{
public MyFileSystemWatcher()
{
Init();
}
public MyFileSystemWatcher(String inDirectoryPath)
: base(inDirectoryPath)
{
Init();
}
public MyFileSystemWatcher(String inDirectoryPath, string inFilter)
: base(inDirectoryPath, inFilter)
{
Init();
}
private void Init()
{
IncludeSubdirectories = true;
// Eliminate duplicates when timestamp doesn't change
NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; // The default also has NotifyFilters.LastWrite
EnableRaisingEvents = true;
Created += Watcher_Created;
Changed += Watcher_Changed;
Deleted += Watcher_Deleted;
Renamed += Watcher_Renamed;
}
public void Watcher_Created(object source, FileSystemEventArgs inArgs)
{
Log.WriteLine("File created or added: " + inArgs.FullPath);
}
public void Watcher_Changed(object sender, FileSystemEventArgs inArgs)
{
Log.WriteLine("File changed: " + inArgs.FullPath);
}
public void Watcher_Deleted(object sender, FileSystemEventArgs inArgs)
{
Log.WriteLine("File deleted: " + inArgs.FullPath);
}
public void Watcher_Renamed(object sender, RenamedEventArgs inArgs)
{
Log.WriteLine("File renamed: " + inArgs.OldFullPath + ", New name: " + inArgs.FullPath);
}
}
}
Notice that each method is logging. We will implement this log next.
Step 3 – Add logging
- Add the class from a previous post: A simple Log singleton in C#
- Make sure to change the namespace to match.
Step 4 – Implement the Service
- Right-click on the Service1.cs file and choose View Code.
- Change both the Name and the ServiceName to DirectoryMonitoringService. You can right-click on the file to rename. If that doesn’t rename the class, you can open the file, right-click on the class name and choose Refactor | Rename.
- Go to the code of the DirectoryMonitoringService.cs file (which was Service1.cs just a couple steps ago) in Visual Studio.
- Implement the constructor as follows:
using System.IO;
using System.ServiceProcess;
namespace DirectoryMonitoring
{
public partial class DirectoryMonitoringService: ServiceBase
{
protected FileSystemWatcher Watcher;
// Directory must already exist unless you want to add your own code to create it.
string PathToFolder = @"C:\Directoy\To\Monitor";
public DirectoryMonitoringService()
{
Log.Instance.LogPath = @"C:\ProgramData\DirectoryMonitoring";
Log.Instance.LogFileName = "DirectoryMonitoring";
Watcher = new MyFileSystemWatcher(PathToFolder);
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
Step 5 – Create a Service Installer
- Right-click on DirectoryMonitoringService.cs and choose View Designer.
- Right-click anywhere in the designer window and choose Add Installer. This adds a ProjectInstaller.cs file.
- Right-click on ProjectInstaller.cs and choose View Designer.
- In the designer, right-click on serviceProcessInstaller1 and choose Properties.
- In the properties, set Account to LocalSystem.
- Back in the designer, right-click on serviceInstaller1 and choose Properties.
- Set StartType to Automatic.
- Add a descriptions if you want.
Step 6 – Install the Service
- Open the Developer Command Prompt by right-clicking and choosing Run as Administrator.
Note: If you don’t have the Developer Command Prompt, you can open a normal command prompt as administration and get installUtil.exe from this path:c:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe
- In the command prompt, change to the bin\debug folder in your project directory.
- Run this command to install the service:
installutil.exe DirectoryMonitoring.exe
- Start the service with this command.
net start DirectoryMonitoringService
Step 7 – Debug the Service
- Make sure the service is started and running.
- In Visual Studio with the DirectoryMonitoring project open, click Debug | Attach to Process.
- Select the DirectoryMonitoring.exe file.
- Put a break point at each event in the MyFileSystemWatcher object.
- Test all four events:
- Add a file.
- Rename a file.
- Open and save a file.
- Delete a file.
You have now created a service to monitor a directory and you have seen how to debug it.

