Skip to main content

Real-Time File and Folder Monitoring in VB.NET: A Comprehensive Guide

3 min read
Share:
On this page (3sections)

Real-Time File and Folder Monitoring in VB.NET: A Comprehensive Guide

In today’s digital landscape, keeping track of file and folder activities is essential for various applications and systems. This article provides an updated and detailed guide on how to monitor file and folder activity in VB.NET. We will explore the FileSystemWatcher class and its events, including Created, Changed, Deleted, Renamed, and Error. By the end of this guide, you will have a solid understanding of how to implement file and folder monitoring in your VB.NET projects.

Getting Started:

To begin, create a new project in the latest version of Visual Studio. In this guide, we will be using VB.NET as the programming language.

Creating Controls and Naming:

For demonstration purposes, let’s create the following controls in the project:

  • Form: Name it “Backup”

  • TextBox: Name it “txt_Spath” (to display the selected path)

  • Button: Name it “btn_Browse” (to browse for the folder)

  • Folder Browser Dialog: Name it “dlg_Fb” (to enable folder selection)

File Monitoring with detailed explanation

Let’s dive into the code and implement the file and folder monitoring functionality. Start by importing the necessary namespaces:

Imports System.IO

Imports System.Diagnostics

Next, define the Backup class and declare a FileSystemWatcher variable:

Public Class Backup

    Public watchfolder As FileSystemWatcher

Handle the click event of the “btn_Browse” button to select the folder for monitoring:

Private Sub btn_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Browse.Click

    If dlg_Fb.ShowDialog = Windows.Forms.DialogResult.OK Then

        txt_Spath.Text = dlg_Fb.SelectedPath.ToString

        watchfolder = New System.IO.FileSystemWatcher()

        watchfolder.Path = txt_Spath.Text

        watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName

        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _

                                   IO.NotifyFilters.FileName

        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _

                                   IO.NotifyFilters.Attributes

        AddHandler watchfolder.Changed, AddressOf _LogChange

        AddHandler watchfolder.Created, AddressOf _LogChange

        AddHandler watchfolder.Deleted, AddressOf _LogChange

        AddHandler watchfolder.Renamed, AddressOf _LogRename

        watchfolder.EnableRaisingEvents = True

    End If

End Sub

Implement the event handler for file and folder changes, and display appropriate messages:

Private Sub _LogChange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)

    Dim Ed As String

    If e.ChangeType = IO.WatcherChangeTypes.Changed Then

        Ed = “File ” + e.FullPath.ToString + ” has been changed”

    End If

    If e.ChangeType = IO.WatcherChangeTypes.Created Then

        Ed = “File ” + e.FullPath.ToString + ” has been created”

    End If

    If e.ChangeType = IO.WatcherChangeTypes.Deleted Then

        Ed = “File ” + e.FullPath.ToString + ” has been deleted”

    End If

    MsgBox(Ed.ToString)

End Sub

Additionally, include an event handler for file renaming:

Public Sub _LogRename(ByVal source As Object, ByVal e As System.IO.RenamedEventArgs)

    Dim Ex As String

    Ex = “File ” + e.OldName.ToString + ” has been renamed to ” + e.Name.ToString

    MsgBox(Ex.ToString)

End Sub

Notes and Reminders

  1. The FileSystemWatcher class allows you to monitor file and folder activities in real-time.

  2. Ensure that you have the necessary permissions to access the folders you want to monitor.

  3. You can customize the actions taken when specific events occur, such as logging the changes to a file or triggering additional processes.

  4. Remember to handle the Error event of the FileSystemWatcher class to handle any errors that may occur during monitoring.

  5. With the updated code and explanations provided, you now have a comprehensive guide on how to monitor file and folder activity in VB.NET. Experiment with the code, explore additional features of the FileSystemWatcher class, and adapt it to suit your specific requirements.

Related Tutorials

Search tutorials