C# Program to Trap Events From File


Welcome to our comprehensive guide on creating a C# program to trap events from a file. Whether you are a beginner or an intermediate C# programmer, this article will provide you with the knowledge and skills to effectively use C# for file event handling.

Introduction to File Events

File events are system-level notifications that occur when a file or directory is created, modified, deleted, or renamed. Monitoring these events allows a program to react to changes in the file system, which can be useful in a variety of scenarios such as log monitoring, file synchronization, and more.

Understanding FileSystemWatcher

In C#, the FileSystemWatcher class is used to listen for changes in the file system. It can watch for changes in files and directories and trigger events when such changes occur.

FileSystemWatcher offers several events, including −

  • Created − Occurs when a file or directory is created in the specified path.

  • Changed − Occurs when a file or directory in the specified path is changed.

  • Deleted − Occurs when a file or directory is deleted from the specified path.

  • Renamed − Occurs when a file or directory in the specified path is renamed.

Creating a C# Program to Trap File Events

Now, let's create a C# program that uses FileSystemWatcher to monitor a specific directory and print a message whenever a file is created, modified, or deleted.

using System;
using System.IO;

class Program {
   static void Main() {
      // Specify the directory to monitor
      string directory = @"C:\temp";
      
      // Create a new FileSystemWatcher
      FileSystemWatcher watcher = new FileSystemWatcher(directory);
      
      // Register event handlers
      watcher.Created += (s, e) => Console.WriteLine($"File created: {e.FullPath}");
      watcher.Changed += (s, e) => Console.WriteLine($"File changed: {e.FullPath}");
      watcher.Deleted += (s, e) => Console.WriteLine($"File deleted: {e.FullPath}");
      
      // Start monitoring
      watcher.EnableRaisingEvents = true;
      
      // Wait for user to quit program
      Console.WriteLine("Press 'q' to quit the sample.");
      while (Console.Read() != 'q') ;
   }
}

In this program, we first specify the directory to monitor. We then create a FileSystemWatcher for that directory and register event handlers for the Created, Changed, and Deleted events. Each event handler is a lambda function that prints a message to the console. We then start monitoring by setting EnableRaisingEvents to true.

Advanced FileSystemWatcher Features

The FileSystemWatcher class also offers advanced features for more granular control over file monitoring. For example, you can use the Filter property to specify the types of files to watch. You can also use the NotifyFilter property to specify the kinds of changes to watch for, such as changes in the file's size, last write time, or security settings.

Conclusion

In this tutorial, we've learned how to create a C# program to trap file events using the FileSystemWatcher class. This class provides a simple yet powerful tool for monitoring changes in the file system, enabling your applications to respond in real-time to file or directory creations, modifications, deletions, and more.

While we've covered the basics, there are more advanced features in FileSystemWatcher that are worth exploring. You can fine-tune the types of changes to watch for or the types of files to monitor, giving you more granular control over your file system monitoring tasks.

Monitoring file events is a common requirement in many applications, and C# makes this task straightforward with its built-in classes and event-driven programming model. By understanding how to use FileSystemWatcher, you've gained a valuable tool in your C# programming toolkit.

Updated on: 24-Jul-2023

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements