Monitor File System via C# FileSystemWatcher

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

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:

  1. Specify the Folder path to monitor..
  2. If you want to monitor recursively, place a check mark on the Recursive checkbox.
  3. Decide the kind of action.
  4. 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

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

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.

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:

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:

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:

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:

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:

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:

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



Categories: C#

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: