Watching Folder Activity in VB.NET - Building Our Application (contd.) (Page 3 of 4 )
The FileSystemWatcher class raises five events, which are Created, Changed, Deleted, Renamed and Error. Because Created, Changed, and Deleted events share the same event signature, we can write just one event handler. We will also write one event handler for Renamed, because its event signatures are different.
Lets type code for handling Created, Changed, and Deleted events raised by the FileSystemWatcher class.
[Note] You will have to type the event declaration, as this procedure is not generated automatically [End Note]
Private Sub logchange(ByVal source As Object, ByVal e As _ System.IO.FileSystemEventArgs) If e.ChangeType = IO.WatcherChangeTypes.Changed Then txt_folderactivity.Text &= "File " & e.FullPath & _ " has been modified" & vbCrLf End If If e.ChangeType = IO.WatcherChangeTypes.Created Then txt_folderactivity.Text &= "File " & e.FullPath & _ " has been created" & vbCrLf End If If e.ChangeType = IO.WatcherChangeTypes.Deleted Then txt_folderactivity.Text &= "File " & e.FullPath & _ " has been deleted" & vbCrLf End If End Sub
This is the code for handling the Renamed event, risen by the FileSystemWatcher class:
Public Sub logrename(ByVal source As Object, ByVal e As _ System.IO.RenamedEventArgs) txt_folderactivity.Text &= "File" & e.OldName & _ " has been renamed to " & e.Name & vbCrLf End Sub
Lastly, here's is the code for the btn_stop_click, which will stop the monitor:
Testing Our Application Now it's the time to run the application and see it in action. Build and run the application, type the folder you want to monitor into the text box and click start watching to start watching that folder.
In the folder you specified, create a file, rename it, update it and delete it to see our application recording those changes, as shown below:
Useful Information Use the FileSystemWatcher.Filter property to determine what files should be monitored. Setting the filter property to "*.txt" will monitor all files with a txt extension. The default is *.*, which means that all files will be monitored. If you want to monitor all files -- with and without extensions -- set the Filter property to "".
FileSystemWatcher can be used to watch files on a local computer, a network drive, or a remote computer, but it does not raise events for a CDROM drive. It only works on Windows 2000 and Windows NT 4.0, and common file system operations might raise more than one event. For example, when a file is edited or moved, more than one event might be raised. Likewise, some anti virus or other monitoring applications can cause additional events.
The FileSystemWatcher class will not watch the specified folder until the path property has been set and EnableRaisingEvents is set to true. Set the FileSystemWatcher.IncludeSubdirectories property to true if you want to monitor subdirectories; otherwise, set it to false. The default is false.
The FileSystemWatcher.Path property supports universal naming convention (UNC) paths. If the folder that the path points to is renamed, then the FileSystemWatcher will reattached itself to the new renamed folder – a very handy function indeed!