Listing 11.18 details the event handlers of the sample applications. Because the Changed, Created, and Deleted event handlers have the same arguments, you can write a common handler for all three and a separate one for the Renamed event. You need not program all the events, only the ones you want to monitor. Now you must program the handlers of the FileSystemWatcher component. IO.NotifyFilters.LastAccess Or IO.NotifyFilters.FileNameįileSystemWatcher1.EnableRaisingEvents = TrueįileSystemWatcher1.EnableRaisingEvents = False IO.NotifyFilters.CreationTime Or IO.NotifyFilters.LastWrite Or _ MsgBox( "Please specify the file types to monitor")įileSystemWatcher1.Filter = įileSystemWatcher1.Path = MsgBox( "Please select a folder to monitor for file changes")Įnd If If = 0 Then If Button1.Text = "Start Monitoring" Then Private Sub Button1_Click(ByVal sender As System.Object, _īyVal e As System.EventArgs) Handles Button1.Click When clicked for the first time, its caption changes to Stop Monitoring if you click it again, it will stop monitoring the file system. When a file is renamed, the program prints both the old and the new name. The type of change is reported to the event handler through the ChangeType member of the e argument. These changes will be signaled though the component’s events, which are programmed to print in the ListBox control at the bottom of the form the type of change detected and the name of the corresponding file. Figure 11.3 – FileSystemWatcher ExampleĪfter setting the Path, Filter and NotifyFilter properties, the code sets the component’s EnableRaisingEvents property to True to start watching for changes. You can create, edit, rename, and then delete a few text files in the root folder to test the application. I’ve chosen the root folder because it’s easy to locate and it has very few files on most systems. This button’s Click event handler prepares the FileSystemWatcher component to monitor changes in text files on the root of the C: drive. The FileSystemWatcher component is initialized when the Start Monitoring button is clicked. The FileSystemWatcher project, shown in Figure 11.3, demonstrates how to set up a FileSystemWatcher component and how to process the events raised by the component.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |