Imports System.IO Public Class FileSystemMonitor Public Shared Sub Main() Dim fsw As New FileSystemWatcher() ' create an object of FileSystemWatcher ' set properties of FileSystemWatcher object fsw.Path = "c:\" fsw.IncludeSubdirectories = True ' add event handlers AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created) AddHandler fsw.Deleted, New FileSystemEventHandler(AddressOf File_Deleted) fsw.EnableRaisingEvents = True ' enable monitoring Console.WriteLine("Started Monitoring C:\ folder. Press enter key to stop.") Console.ReadLine() End Sub ' event handler to handle created event Public Shared Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs) Console.WriteLine(e.FullPath & " - Created") End Sub ' event handler to handle deleted event Public Shared Sub File_Deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs) Console.WriteLine(e.FullPath & " - Deleted") End Sub End Class
Imports System.IO Public Class Service1 Dim fsw As FileSystemWatcher Dim fw As StreamWriter Protected Overrides Sub OnStart(ByVal args() As String) fw = New StreamWriter(Environment.GetEnvironmentVariable("temp") & "\filesystem.log") fsw = New FileSystemWatcher() If args.Length > 0 Then fsw.Path = args(0) fsw.IncludeSubdirectories = True Else ' default settings fsw.Path = "c:\" fsw.IncludeSubdirectories = False End If AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created) AddHandler fsw.Deleted, New FileSystemEventHandler(AddressOf File_Deleted) fsw.EnableRaisingEvents = True End Sub Public Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs) fw.WriteLine(Now.ToShortDateString & " " & Now.ToShortTimeString & " - " & e.FullPath & " - Created") End Sub Public Sub File_Deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs) fw.WriteLine(Now.ToShortDateString & " " & Now.ToShortTimeString & " - " & e.FullPath & " - Deleted") End Sub Protected Overrides Sub OnStop() fw.Close() End Sub End Class
ServiceName : FSMonitor DispalyName : File System Monitor StartType : Manual (default)
Account : LocalSystem
Installutil FSMonitor.exe
InstallUtil /u FSMonitor.exe