Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

Monitoring File System Events

Introduction

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
            fw.WaitForChanged(WatcherChangeTypes.All)
        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
    RenamedEventArgs)
        Dim strMsg As String
        strMsg = "Old Path :" & e.OldFullPath() & vbCrLf
        strMsg += "New Path :" & e.FullPath
        MessageBox.Show(strMsg)
    End Sub

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

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

    Public Sub MyDeletedHandler(ByVal s As Object, ByVal e As
    FileSystemEventArgs)
        Dim strMsg As String
        strMsg = "File/Folder Deleted :" & e.FullPath
        MessageBox.Show(strMsg)
    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 "bipinjoshi@yahoo.com","mailto:bipinjoshi@yahoo.com",true,false %>

Keep coding !




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

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 07 Dec 2001



Tags : .NET Framework Windows Forms Components Programming Languages