Link Search Menu Expand Document

How to Monitor File or Directory using FileSystem Watcher in C#


The FileSystemWatcher, as its name suggests, is a component class in the .net framework that continuously monitors your files. You can define files or a whole directory to look after and have a custom action that notifies you every time those files/directory have been changed. In this video, we’ll be going to perform one demo of file system watcher.

In this session, we’ll be going to perform one demo of the File System Watcher. The File System Watcher as its name suggests is a component class in Dot Net Framework that continuously monitors your files or configured folders. You can define files or a whole directory to look after and have a custom action that notifies you every time those files or directly have been changed. Here we are creating a File System Watcher object called a File System Watcher. We also Parsing path of the directory which we want to monitor in the constructor of the file system watcher class. Then we have set a filter. In our case, it’s (“*.txt”) which means here we want to monitor only dot text files.

Then I have set a notify filter for the file system that we are interested in being notified of. For example, in our case, it’s the file name, file size, and file attribute.  I have just created a filter to notify us when there are changes to the file name, file size, and the attributes such as read-only or hidden. We get notified of the changes by hooking up an event handler like change (fileSystemWatcher.Changed +=OnActionOccurOnFolderPath;) or create, delete and rename events of the file system watcher. For the first three events, we are just pointing to the same event handler because they have the same type of event ARG [(private static void OnActionOccurOnFolderPath(object sender, FileSystemEventArgs e)]. For the rename event, we are hooking this [(private static void OnFileRenameOccur(object sender, RenamedEeventArg e)] up to the on-file Rename Occur Method. Then we set the ‘Enable Raising Events’ to true. It will start monitoring and listening to this event handler method.

Programmers Essentials Directory

Now, if we look at this method, we are just writing out the change type and the file name. In the case of the rename method, we are just writing out the old file name using this(Console.WriteLine($”Old file name => {e.OldName}”) property and the new file name using this(Console.WriteLine($”New file name => {e.Name}”) property in the console. Now let’s run the application and have a look at the directories that we are monitoring. Create a new text file. Notice here in the console, we have got our file changed event and the type of the file event is created.

Rename the file and you can see that we get our final rename event handler. We have our old filename and our new file name as well. Same way when I delete this file, we get the deleted event and the file name. Now, let me recreate a new text file. Open the property window of this file. Now, in this attribute, if I change any of these two attributes, for example, if I marked the file as read-only and click on the apply button, you can see that our change event is a file.

Programmers Essentials Directory Monitoring

Now if I uncheck the property and click on apply button, this file change event is again fired. The File System Watcher is a very handy class when you want to monitor the file system of specific directories and files.

For more videos and articles visit:

👉 GET YOUR FREE API KEY for PDF.co

https://app.pdf.co/signup?utm_source=youtube

✅ ON-PREMISE SDK FREE TRIAL:

https://bytescout.com/download/web-installer?utm_source=youtube

🔎 SDK DOCUMENTATION:

https://bytescout.com/documentation/

🔑 WEB API DOCUMENTATION :

https://pdf.co/rest-web-api