![]() You then create four event handlers for each possible file change. This allows the watcher to keep watching while the form is open. Here, you set the SynchronizingObject to the current form. Set the properties for the FileSystemWatcher.įsWatch.Changed = new FileSystemEventHandler(LogFile) įsWatch.Created = new FileSystemEventHandler(LogFile) įsWatch.Deleted = new FileSystemEventHandler(LogFile) įsWatch.Renamed = new RenamedEventHandler(LogRename) įileSystemEventHandler(AddressOf LogFile) VB.NET Private fsWatch As FileSystemWatcher Name them anything you like, but keep in mind that my names may differ from yours.Īdd the following namespaces to your code. Two textboxes with their Text properties set.Once your form has loaded, design it to resemble Figure 1. Open Visual Studio 2019 and create either a new C# or VB.NET Windows Forms application. These changes could be things like file deletion, file renaming, changing the properties of a file, and so on. To put it better: It watches a folder for any changes. What Is FileSystemWatcher?Īs the name implies, it watches the system for files. Today, I would like to talk about using the FileSystemWatcher in. ![]() 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.Welcome to my article. You would observe that the name of the newly created file is displayed in the console window. Private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)Ĭonsole.WriteLine("File created: ", e.Name) Īssuming 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. In essence, this starts the actual monitoring - you are informing FileSystemWatcher to start monitoring the path and raise appropriate events henceforth.įor 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. 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. Private static void MonitorDirectory(string path)įileSystemWatcher fileSystemWatcher = new FileSystemWatcher() įileSystemWatcher.Created = FileSystemWatcher_Created įileSystemWatcher.Renamed = FileSystemWatcher_Renamed įileSystemWatcher.Deleted = FileSystemWatcher_Deleted įileSystemWatcher.EnableRaisingEvents = true The directory path is passed as an argument to the method. This method would be used to monitor a particular directory and raise events whenever a change occurs. The following code snippet shows how the MonitorDirectory method would look like. 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. Note that a better way to use the FileSystemWatcher class would be by using a Windows Service. Let's create a new console application project in Visual Studio to demonstrate how a typical file system watcher works. Renamed: This event is triggered when a file or a directory in the path being monitored is renamedĬreating a simple file system watcher in C#.Error: This event is triggered there is an error due to changes made in the path being monitored.Deleted: This event is triggered when a file or a directory in the path being monitored is deleted.Created: This event is triggered when a file or a directory in the path being monitored is created.Changed: This event is triggered when a file or a directory in the path being monitored is changed.The FileSystemWatcher raises the following events when changes occur to a directory that it is monitoring. In order for the FileSystemWatcher to work, you should specify a directory that needs to be monitored. It watches a file or a directory in your system for changes and triggers events when changes occur. The FileSystemWatcher class in the System.IO namespace can be used to monitor changes to the file system.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |