Watching Folder Activity in VB.NET

Building Our Application

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

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:

' Stop watching the folder
watchfolder.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False

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!

You might also like...

Comments

About the author

Jayesh Jain

Jayesh Jain New Zealand

Jayesh Jain is working as a Business Analyst in Auckland, New Zealand. He has several years of n-Tier development experience in developing interactive client solutions. He has a passion for Web ...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay