C# - File System Watcher
In C#, the 'FileSystemWatcher' class from the 'System.IO' namespace allows you to monitor file system events such as file creation, modification, deletion, and renaming. It enables your application to respond to changes in real-time. Let's explore how to use the 'FileSystemWatcher' class to monitor file system events and implement event handlers:
Creating a FileSystemWatcher Instance:
To begin monitoring file system events, create an instance of the 'FileSystemWatcher' class and set its properties as needed.
using System.IO;
// Create a new instance of FileSystemWatcher
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
Setting Path and Filter Properties:
Specify the directory path that you want to monitor using the 'Path' property. Additionally, you can use the Filter property to specify the file name pattern (wildcards can be used).
fileSystemWatcher.Path = @"C:\MyFolder";
fileSystemWatcher.Filter = "*.txt"; // Watch only .txt files (optional)
Subscribing to Events:
To respond to file system events, you need to subscribe to event handlers provided by the 'FileSystemWatcher' class.
fileSystemWatcher.Created += OnFileCreated;
fileSystemWatcher.Changed += OnFileChanged;
fileSystemWatcher.Deleted += OnFileDeleted;
fileSystemWatcher.Renamed += OnFileRenamed;
Implementing Event Handlers:
Define the event handlers for the respective file system events. These handlers will be called when the corresponding events occur.
private static void OnFileCreated(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File created: {e.Name} - {e.FullPath}");
}
private static void OnFileChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File changed: {e.Name} - {e.FullPath}");
}
private static void OnFileDeleted(object sender, FileSystemEventArgs e)
{
Console.WriteLine($"File deleted: {e.Name} - {e.FullPath}");
}
private static void OnFileRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"File renamed: {e.OldName} - {e.FullPath}");
Console.WriteLine($"New file name: {e.Name}");
}
Starting and Stopping the 'FileSystemWatcher':
Before monitoring, you need to start the 'FileSystemWatcher' using the 'EnableRaisingEvents' property. Once you're done, you can stop it by setting this property to false.
fileSystemWatcher.EnableRaisingEvents = true; // Start monitoring
// Your application logic...
fileSystemWatcher.EnableRaisingEvents = false; // Stop monitoring
Handling Exceptions:
It's important to handle exceptions while using 'FileSystemWatcher' as file system events may trigger rapidly, and any exceptions left unhandled may disrupt your application.
try
{
// Code involving the FileSystemWatcher instance and event handling
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
Keep in mind that the 'FileSystemWatcher' class may fire multiple events for a single file system change. It's recommended to use appropriate filters and logic to handle these situations according to your application's needs.
By using the 'FileSystemWatcher', you can create applications that respond to real-time changes in the file system, making it useful for tasks such as file synchronization, data processing, and more.