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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos