December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

Monitoring File System Events


In some applications like file system explorers, file managers etc. it becomes necessary to keep an eye on file system changes. In traditional tools like VB6 this task usually required some third party component or Windows APIs. In .NET the FileSystemWatcher class from System.IO namespace provides easy way to do the same thing. A windows file system has many events associated with files and folders. They include creation, renaming, deletion, change etc. In this article we will see how to use FileSystemWatcher class.

Steps involved in file system monitoring

Following steps are involved in typical application that uses FileSystemWatcher class:
Import required namespace
The class FileSystemWatcher resides in System.IO namespace. So, you need to import this namespace. In addition to System.IO you will also need some threading support since you will be continuously monitoring for any change. So, you will also need System.Threading namespace.
Imports System.IO
Imports System.Threading
Create object of FileSystemWatcher class and set its properties
    Dim fw As New FileSystemWatcher()
    fw.Path = "c:\bipin"
    fw.Filter = "*.*"
    fw.IncludeSubdirectories = True

  • The path property sets the location that is to be monitored.
  • The filter property sets the type of files to be monitored.
  • The IncludeSubdirectories property decides whether changes in sub-folders are to be considered for monitoring or not.
Attach event handlers for various events
Next, we will attach event handlers for various events as shown below :
AddHandler fw.Renamed, AddressOf MyRenamedHandler
AddHandler fw.Created, AddressOf MyCreatedHandler
AddHandler fw.Changed, AddressOf MyChangedHandler
AddHandler fw.Deleted, AddressOf MyDeletedHandler
Here, we have added event handlers to Renamed, Created, Changed and Deleted events using AddHandler statement.
Watch for any changes
Now that we have configured our FileSystemWatcher object it is time to start monitoring the file system. For our example I have put monitoring on a separate thread.
    Dim t As New Thread(AddressOf WatchChange)
    Public Sub WatchChange()
        While True
        End While
    End Sub
The WaitForChanged method is invoked inside an end less while loop. The argument to the method tells that we are interested in all types of changes.
Write event handlers
The final thing is to write event handlers for various methods mentioned above.
    Public Sub MyRenamedHandler(ByVal s As Object, ByVal e As
        Dim strMsg As String
        strMsg = "Old Path :" & e.OldFullPath() & vbCrLf
        strMsg += "New Path :" & e.FullPath
    End Sub

    Public Sub MyCreatedHandler(ByVal s As Object, ByVal e As
        Dim strMsg As String
        strMsg = "File/Folder Created :" & e.FullPath
    End Sub

    Public Sub MyChangedHandler(ByVal s As Object, ByVal e As
        Dim strMsg As String
        strMsg = "File/Folder Changed :" & e.FullPath
    End Sub

    Public Sub MyDeletedHandler(ByVal s As Object, ByVal e As
        Dim strMsg As String
        strMsg = "File/Folder Deleted :" & e.FullPath
    End Sub

Here, I am simply displaying the path of item (file or folder) being changed but you can add whatever functionality you wish. In order to test the application run the application and change some file names in the explorer. You should get a message box indicating the changes.

I hope you must have found the article interesting. Send your feedback and suggestions at <% displaylink "","",true,false %>

Keep coding !

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 07 December 2001

Tags : .NET Framework Windows Forms Components Programming Languages