In C#, if you want to monitor the changes in files in a folder, such as file creation, deletion, modification, etc., you can useFileSystemWatcher
kind.FileSystemWatcher
Classes provide an easy way to monitor file system changes. It is locatedIn the namespace, and allows you to specify the directory to monitor and the type of event you are interested in.
1. Introduction to FileSystemWatcher class
FileSystemWatcher
Classes provide an asynchronous mechanism to monitor file system changes. You can register an event handler through it to respond to changes in files or directories, such as:
- Created: Occurs when a new file or directory is created in the specified directory.
- Deleted: Occurs when a file or directory is deleted in a specified directory.
- Changed: Occurs when changing files in the specified directory.
- Renamed: Occurs when renaming a file or directory in a specified directory.
2. Use FileSystemWatcher class
To useFileSystemWatcher
Class, you need:
- Create a
FileSystemWatcher
Example. - Set its Path property to specify the directory to monitor.
- (Optional) Set other properties, such as Filter, NotifyFilter, etc., to filter and customize monitoring.
- Register an event handler for events of interest.
- Call
EnableRaisingEvents
Method starts monitoring.
3. Sample code
Here is a simple example showing how to use itFileSystemWatcher
Class to monitor the changes in files in a folder:
using System; using ; class Program { static void Main() { // Create FileSystemWatcher instance FileSystemWatcher watcher = new FileSystemWatcher(); // Set the directory to monitor = @"C:\YourFolderToWatch"; // Filter conditions, such as monitoring only .txt files = "*.txt"; // Notification filter, set the event type to be monitored = | ; // Register event handler += OnFileCreated; += OnFileDeleted; += OnFileChanged; += OnFileRenamed; // Start monitoring = true; // Keep the console on to receive events ("Press 'Enter' to quit the sample."); (); // Stop monitoring = false; } // Triggered when the file is created private static void OnFileCreated(object source, FileSystemEventArgs e) { ($"File: {} has been created."); } // Triggered when the file is deleted private static void OnFileDeleted(object source, FileSystemEventArgs e) { ($"File: {} has been deleted."); } // Triggered when the file is modified private static void OnFileChanged(object source, FileSystemEventArgs e) { ($"File: {} has been changed."); } // Triggered when the file is renamed private static void OnFileRenamed(object source, RenamedEventArgs e) { ($"File: {} has been renamed to {}."); } }
In this example,FileSystemWatcher
The instance is set to monitorC:\YourFolderToWatch
All in the directory.txt
File creation, deletion, modification, and renaming events. Whenever these events occur, the corresponding event processor is called and the corresponding message is output on the console.
4. Things to note
- Make sure your application has permission to access and monitor specified folders.
-
FileSystemWatcher
All file system events may not be captured, especially in high load or high concurrency.
To reduce resource usage, it can be used after event processing is completedStop()
Method to stopFileSystemWatcher
and restart it if needed. - If your application needs to handle a large number of file or directory events, you may want to consider using other mechanisms, such as
Windows API
Call, or use third-party libraries to enhance performance.
5. Summary
FileSystemWatcher
Classes are a powerful and easy-to-use tool for monitoring file changes in folders in C#. By using it reasonably, you can implement functions such as automatic backup, logging, real-time synchronization, etc. When using it, make sure to handle events well and take into account performance and resource usage factors.
This is the article about the implementation of monitoring folders of FileSystemWatcher class in C#. For more related content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!