1. Introduction to C# FileSystemWatcher Component
C# FileSystemWatcher Component will track the changes in the file system. Simply we can create a decent looking Monitor File utility within one hour. The changes may be file creation, deletion, Modification and renaming. In this example, we will create an app that can spy on your file system based on the folder path that we mention. Whenever a change is found in the file system, the control gives a signal to the listening application through the FileSystemWatcher events.
2. About The Folder Monitor Example
The below screenshot shows the design of this FileSystemWatcher Example:

Monitor File System – FileSystemWatcher Component Example
The top screen will show the Spied content. This wide white screen is a multi-line text box. Folder Picker button will allow us to pick a folder path to be monitored. Once we picked a folder, our example will display the folder name in the Folder Path to Monitor text box. The Recursive check box tells that we want to monitor the sub-folders of the folder picked by the folder picker dialog. Say, for example, if we picked ‘C:\’ through the folder picker and checked the Recursive checkbox, then our example will monitor the entire ‘C:\’ partition or drive.
Start Monitor button of the FileSystemWatcher Example will start the audit process. There is not much validation done here, so the sequence is below:
- Specify the Folder path to monitor..
- If you want to monitor recursively, place a check mark on the Recursive checkbox.
- Decide the kind of action.
- Finally, click the monitor button.
Clear Content button will erase the spied content displayed on the output screen at any time. Close button will exit this example. The four Checkbox Filters are useful for specifying what kind of action we want to track. It is simple to create this sample as much of the work is done by the FileSystemWatcher Component.
3. Preparing the Windows Form For FileSystemWatcher
From fig.1 you can place the needed controls to the form. This example uses the basic controls apart from the FileSystemWatcher Component. So, it is not a big deal to design this form. To make the sample, we need two components. One is ‘FolderBrowserDialog Component’ and another one is ‘FileSystemWatcher Component’. C# FileSystemWatcher is the key component that will spy the file system on the folder that we point out. The below picture shows from where one can drag and drop these components.

Components Required For This Example
In our sample, FolderB is the name given to the C# FolderBrowserDialog and FileMon is the name given to the C# FileSystemWatcher component. Once the items are dropped to windows form, we need to add four event handlers for the FileSystemWatcher (FileMon) component. The picture below shows the event handler kept for the FileMon:

Events of FileSystemWatcher Component
4. Source Code Implementation of FileSystemWatcher Example
4.1 Variables For This Example
First, we declare the required variables. We will see the use of it when we progress through the coding.
1 2 3 |
//Sample 01: Declarations. private string content = ""; private string last_event_filename = ""; |
4.2 Close & Clear Button Click Handler
Event handler provided for the close and Clear Content is straightforward. The code for that is given below:
1 2 3 4 5 6 7 8 9 10 11 |
//Sample 02: Close the form private void btnClose_Click(object sender, EventArgs e) { fileMon.EnableRaisingEvents = false; this.Close(); } //Sample 03: Clear Text Content private void btnClear_Click(object sender, EventArgs e) { txtDisplay.Clear(); } |
4.3 IncludeSubdirectories & Path Properties
The C# FileSystemWatcher Component will spy the files under a given path. To spy the files inside the sub-folder we should set
IncludeSubdirectories
Property to true. We can set this property only when we have a valid path and it is set through the
Path
Property. In our example, we provided a checkbox that will set
IncludeSubdirectories
property to ‘true or false’ based on its check state. Below is the code:
1 2 3 4 5 6 7 8 |
//Sample 04: Do it recursively private void chkRecursive_CheckedChanged(object sender, EventArgs e) { if (chkRecursive.Checked == true ) fileMon.IncludeSubdirectories = true; else fileMon.IncludeSubdirectories = false; } |
4.4 Getting Folder Path To Monitor
In the previous step we saw the code that set the
IncludeSubdirectories
and this property of the FileSystemWatcher Component become meaningful only when we have a valid path to monitor. The
Path
Property of the component will be used to specify from where exactly we want to monitor the files. To set this
Path
property, we use
FolderBrowserDialog
Component from the dialog group. When the ‘…’ button is clicked, we make use of this component to get the Folder Path from the user. Then our example uses this path to monitor the file system. And the Recursive checkbox will tell we need to monitor this selected path, recursively or not. Below is the piece code:
1 2 3 4 5 6 7 8 9 |
//Sample 05: Set the Path to Spy using Folder Browser Dialog private void btnPickFolder_Click(object sender, EventArgs e) { if (FolderB.ShowDialog() == DialogResult.OK) { txtFolderPath.Text = FolderB.SelectedPath; fileMon.Path = txtFolderPath.Text; } } |
4.5 EnableRaisingEvents Property
The C# FileSystemWatcher component will raise the events (Fig.3) when its
EnableRaisingEvents
Property is set to true. Once it raises the events, we will get the spied events in the corresponding handler functions. So, we set this property when we click the Start Monitor button. Below is the piece of code:
1 2 3 4 5 6 |
//Sample 06: Enable capturing file system events private void btnMonitor_Click(object sender, EventArgs e) { fileMon.Path = txtFolderPath.Text; fileMon.EnableRaisingEvents = true; } |
4.6 Enabling Recursive Checkbox
The recursive checkbox will set the
IncludeSubdirectories
property of the component. We will get an application exception when we set this property to true when the
Path
property does not contain any valid path to monitor. So below is the piece of code which looks after this case:
1 2 3 4 5 6 7 8 |
//Sample 07: Enable capturing file system events private void txtFolderPath_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtFolderPath.Text)) chkRecursive.Enabled = false; else chkRecursive.Enabled = true; } |
4.7 File Monitor Handler
Now, we will handle all the four events shown in Fig.3. In each handler, we will get the filename from the
FileSystemEventArgs
, which will come to the handler as a parameter. As we already inside the handler, we know the corresponding action say whether it is a file deletion or Rename, etc. On each handler, we will check the status of the Filter check boxes and display the information only when the corresponding check box is checked. Say, for example, before displaying the file renamed action, we will check whether the user applied the filter or not. Below is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
//Sample 08: Monitor all the File Events private void fileMon_Changed(object sender, System.IO.FileSystemEventArgs e) { //!under_modification: Avoids repeated consecutive ourput of //Files changed per file if (chkModified.CheckState == CheckState.Checked && last_event_filename != e.FullPath) { content = "File Changed: " + e.FullPath + Environment.NewLine; txtDisplay.AppendText(content); } last_event_filename = e.FullPath; } private void fileMon_Created(object sender, System.IO.FileSystemEventArgs e) { if (chkCreated.CheckState == CheckState.Checked) { content = "File Created: " + e.FullPath + Environment.NewLine; txtDisplay.AppendText(content); } last_event_filename = e.FullPath; } private void fileMon_Deleted(object sender, System.IO.FileSystemEventArgs e) { if (chkDeleted.CheckState == CheckState.Checked) { content = "File Deleted: " + e.FullPath + Environment.NewLine; txtDisplay.AppendText(content); } last_event_filename = e.FullPath; } private void fileMon_Renamed(object sender, System.IO.RenamedEventArgs e) { if (chkRenamed.CheckState == CheckState.Checked) { content = "File Renamed: New Name - "+ e.FullPath + " Old Name : " + e.OldFullPath + Environment.NewLine; txtDisplay.AppendText(content); } last_event_filename = e.FullPath; } |
5. Running Our Example
The recorded video shows how we spy the directory D:\Temp. First, we copy the files from C:\Temp to D:\Temp. Then we rename the a file in D:\Temp. Finally, we delete the list of files from the D:\Temp.
One can use this C# Example to spy their entire C:\ drive. Just Select C:\ then click on the recursive button, and the sample is now tracking entire C:\ drive. It is very useful when we are working on extensive projects and want to know how a specific workflow affects the file system on a specific folder and its Child.
Source Code: Download C# FileSystemWatcher Example from Google Drive
- Ranking Functions Examples – RANK, DENSE_RANK & Percentile
- Send Message to Microsoft Message Queue (MSMQ) – C# Example
Categories: C#
Tags: EnableRaisingEvents Property, FileSystemEventArgs, FileSystemWatcher, IncludeSubdirectories Property, Path Property