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 an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a yoga mentor, meditation teacher, and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his private online courses on ASP.NET and meditation go here and here.

Posted On : 07 December 2001

Tags : .NET Framework Windows Forms Components Programming Languages