This is based on the similarly named System.IO.FileSystemWatcher in .Net Framework. Most of the code is hoisted from a fafalone submission here in these forums.
Some testing code in a form for a sample of usage:
Full working project: FileSystemWatcher.zip
cheers,
</wqw>
Code:
' cFileSystemWatcher.cls
'=========================================================================
'
' FileSystemWatcher (c) 2023 by wqweto@gmail.com
'
' Based on code by fafalone at https://www.vbforums.com/showthread.php?805991
'
' Uses self-contained and IDE-safe subclassing on API created window
'
' Emulates FileSystemWatcher in System.IO namespace of .Net Framework
'
'=========================================================================
<too big to post>
See linked FileSystemWatcher.zip
Code:
' Form1.frm
Option Explicit
Private WithEvents m_oWatcher As cFileSystemWatcher
Private Sub Form_Load()
Set m_oWatcher = New cFileSystemWatcher
m_oWatcher.Path = "D:\TEMP"
m_oWatcher.IncludeSubdirectories = True
m_oWatcher.EnableRaisingEvents = True
End Sub
Private Sub Command1_Click()
Dim eType As UcsWatcherChangeTypesBitmask
Dim sName As String
eType = ucsWctAll
If m_oWatcher.WaitForChanged(eType, sName, Timeout:=10000) Then
Debug.Print "WaitForChanged, eType=" & eType & ", sName=" & sName, Timer
End If
End Sub
Private Sub m_oWatcher_OnChanged(ByVal ChangeType As UcsWatcherChangeTypesBitmask, FullPath As String, Name As String)
Debug.Print "OnChanged, ChangeType=" & ChangeType & ", FullPath=" & FullPath & ", Name=" & Name, Timer
End Sub
Private Sub m_oWatcher_OnCreated(ByVal ChangeType As UcsWatcherChangeTypesBitmask, FullPath As String, Name As String)
Debug.Print "OnCreated, ChangeType=" & ChangeType & ", FullPath=" & FullPath & ", Name=" & Name, Timer
End Sub
Private Sub m_oWatcher_OnDeleted(ByVal ChangeType As UcsWatcherChangeTypesBitmask, FullPath As String, Name As String)
Debug.Print "OnDeleted, ChangeType=" & ChangeType & ", FullPath=" & FullPath & ", Name=" & Name, Timer
End Sub
Private Sub m_oWatcher_OnRenamed(ByVal ChangeType As UcsWatcherChangeTypesBitmask, FullPath As String, Name As String, OldFullPath As String, OldName As String)
Debug.Print "OnRenamed, ChangeType=" & ChangeType & ", FullPath=" & FullPath & ", Name=" & Name & ", OldFullPath=" & OldFullPath & ", OldName=" & OldName, Timer
End Sub
cheers,
</wqw>