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

🎯 Introduction

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:

🎯 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