1. Preface
When developing applications, we may monitor files or folders in the file system in real time due to the needs of the scenario, so that we can react in a timely manner when the file content is changed, the file is created, deleted or renamed.
Today, I will introduce you to the complete operation process, allowing you to easily implement the function of monitoring file/folder changes.
This tutorial takes Winform as an example, but it does not mean that the function of listening to folders can only be used by Winform. As long as the C# code can run, it can be used.
2. FileSystemWatcher class
In C#, classes provide us with such a powerful function.
Therefore, we can use this class directly to implement functions in the project. It is worth noting that in the namespace, don’t quote the class incorrectly when you quote it.
3. FolderBrowserDialog class
In C#, the class can provide Winform programs with the function of selecting folders. Here we use this class to select folders.
Of course, it is not impossible if you have to manually copy the path to the folder.
4. Specific operation
1. Set up the listening folder
We create a new form, name it:, and then add a text box control and a button control.
Write the following code in the button click event:
FolderBrowserDialog dialog = new FolderBrowserDialog(); = false; (); if ( != null && > 0) { // Assign to the text box = [0].Trim(); }
In this way, the text box will be assigned a folder path after each folder is selected.
2. Subscribe to change events
public partial class MainWindow : Form { private FileSystemWatcher? _fileWatcher; //The directoryPath here is the folder path selected in the previous step public void StartMonitoring(string directoryPath) { // Create FileSystemWatcher instance _fileWatcher = new FileSystemWatcher(); // Set the directory path to monitor _fileWatcher.Path = directoryPath; // Set the type of change to monitor (for example: modify, create, delete) _fileWatcher.NotifyFilter = | | ; // Add event handler _fileWatcher.Changed += OnChanged; _fileWatcher.Created += OnChanged; _fileWatcher.Deleted += OnChanged; _fileWatcher.Renamed += OnRenamed; // Turn on event monitoring _fileWatcher.EnableRaisingEvents = true; ($"Start monitoring of folders: {directoryPath}"); } private void OnChanged(object source, FileSystemEventArgs e) { ($"File or folder changes: {},Event Type: {}"); } private void OnRenamed(object source, RenamedEventArgs e) { ($"Files or folders are renamed: Old path {} -> New path {}"); } public void StopMonitoring() { if (_fileWatcher != null) { // Stop raising events _fileWatcher.EnableRaisingEvents = false; // Clean up resources _fileWatcher.Dispose(); _fileWatcher = null; ("Stop monitoring folders"); } } }
In the above code, we callStartMonitoring
The method initiates monitoring of the specified directory and triggers the corresponding event handler when a file or subdirectory changes.
NotifyFilter
Attributes are used to define the specific event types that need to be listened to, such as the last write time, the changes in file name and directory name, etc.
Changed
、Created
、Deleted
andRenamed
The four events correspond to the content changes, creation, deletion and renaming operations of files or folders respectively.
3. Things to note
The FileSystemWatcher class does not guarantee the immediacy or sequentiality of events, especially in the case of large number of concurrent file operations, which may merge or delay triggering events.
When using FileSystemWatcher, you should ensure that resources are processed reasonably, especially when monitoring is no longer needed, the Dispose method should be called in time to release resources.
In general, it is a powerful tool for real-time monitoring of file systems. With simple configuration and event processing, our applications can be able to respond sensitively to file system changes.
This is the article about C#’s Winform monitoring folder changes and monitoring file operation tutorial. For more related C# Winform monitoring folder changes and operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!