The FileSystemWatcher class can be used to monitor changes to file system and trigger events when such changes occur Credit: IDG The FileSystemWatcher class in the System.IO namespace can be used to monitor changes to the file system. It watches a file or a directory in your system for changes and triggers events when changes occur. In order for the FileSystemWatcher to work, you should specify a directory that needs to be monitored. The FileSystemWatcher raises the following events when changes occur to a directory that it is monitoring. Changed: This event is triggered when a file or a directory in the path being monitored is changed Created: This event is triggered when a file or a directory in the path being monitored is created Deleted: This event is triggered when a file or a directory in the path being monitored is deleted Error: This event is triggered there is an error due to changes made in the path being monitored Renamed: This event is triggered when a file or a directory in the path being monitored is renamed Creating a simple file system watcher in C# Let’s create a new console application project in Visual Studio to demonstrate how a typical file system watcher works. Note that a better way to use the FileSystemWatcher class would be by using a Windows Service. You can build a Windows Service that uses the FileSystemWatcher class and sends out notifications as and when changes occur to the path being watched. Anyway, let’s now get into a bit of code now. In the Main method of the Program.cs file, write the following code. static void Main(string[] args) { string path = @"D:IDG"; MonitorDirectory(path); Console.ReadKey(); } The following code snippet shows how the MonitorDirectory method would look like. This method would be used to monitor a particular directory and raise events whenever a change occurs. The directory path is passed as an argument to the method. private static void MonitorDirectory(string path) { FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); fileSystemWatcher.Path = path; fileSystemWatcher.Created += FileSystemWatcher_Created; fileSystemWatcher.Renamed += FileSystemWatcher_Renamed; fileSystemWatcher.Deleted += FileSystemWatcher_Deleted; fileSystemWatcher.EnableRaisingEvents = true; } Note how the events are declared and that the EnableRaisingEvents property of the file system watcher object is set to true to enable raising events when a change on the path being monitored occurs. In essence, this starts the actual monitoring — you are informing FileSystemWatcher to start monitoring the path and raise appropriate events henceforth. For each of the events that you have declared, you should have the respective event handler that gets executed when the event is triggered. Here’s the source code of the event handlers that would be triggered as and when a change to the directory being monitored occurs. private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine("File created: {0}", e.Name); } private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e) { Console.WriteLine("File renamed: {0}", e.Name); } private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine("File deleted: {0}", e.Name); } Here’s the complete source code for your reference. using System; using System.IO; namespace IDGFileSystemWatcher { class Program { static void Main(string[] args) { string path = @"D:IDG"; MonitorDirectory(path); Console.ReadKey(); } private static void MonitorDirectory(string path) { FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(); fileSystemWatcher.Path = path; fileSystemWatcher.Created += FileSystemWatcher_Created; fileSystemWatcher.Renamed += FileSystemWatcher_Renamed; fileSystemWatcher.Deleted += FileSystemWatcher_Deleted; fileSystemWatcher.EnableRaisingEvents = true; } private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e) { Console.WriteLine("File created: {0}", e.Name); } private static void FileSystemWatcher_Renamed(object sender, FileSystemEventArgs e) { Console.WriteLine("File renamed: {0}", e.Name); } private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e) { Console.WriteLine("File deleted: {0}", e.Name); } } } Assuming that the directory named IDG is available on the D:> drive of your system, run the console application and then create a new file in the IDG directory. You would observe that the name of the newly created file is displayed in the console window. This is because as soon as a new file is created in the directory being monitored (D:IDG in our example), the FileSystemWatcher_Created event is triggered. Related content feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos