Imports System.IO
Imports System.Windows.Forms
public class FileSystemWatcherDemo
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtPath As System.Windows.Forms.TextBox
Friend WithEvents txtFile As System.Windows.Forms.TextBox
Friend WithEvents txtWatcher As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents btnWatch As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtPath = New System.Windows.Forms.TextBox()
Me.txtFile = New System.Windows.Forms.TextBox()
Me.txtWatcher = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.btnWatch = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.Label1.Location = New System.Drawing.Point(51, 20)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(103, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "File Watcher"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.Label2.Location = New System.Drawing.Point(51, 52)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(103, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "File Type"
'
'txtPath
'
Me.txtPath.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.txtPath.Location = New System.Drawing.Point(164, 16)
Me.txtPath.Name = "txtPath"
Me.txtPath.Size = New System.Drawing.Size(655, 23)
Me.txtPath.TabIndex = 2
Me.txtPath.Text = "C:\Temp\test\"
'
'txtFile
'
Me.txtFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.txtFile.Location = New System.Drawing.Point(164, 48)
Me.txtFile.Name = "txtFile"
Me.txtFile.Size = New System.Drawing.Size(133, 23)
Me.txtFile.TabIndex = 3
Me.txtFile.Text = "*.txt"
'
'txtWatcher
'
Me.txtWatcher.Location = New System.Drawing.Point(51, 104)
Me.txtWatcher.Multiline = True
Me.txtWatcher.Name = "txtWatcher"
Me.txtWatcher.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtWatcher.Size = New System.Drawing.Size(778, 224)
Me.txtWatcher.TabIndex = 5
Me.txtWatcher.Text = ""
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.Label3.Location = New System.Drawing.Point(51, 80)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(103, 16)
Me.Label3.TabIndex = 6
Me.Label3.Text = "Message"
'
'btnWatch
'
Me.btnWatch.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.btnWatch.Location = New System.Drawing.Point(625, 64)
Me.btnWatch.Name = "btnWatch"
Me.btnWatch.Size = New System.Drawing.Size(194, 32)
Me.btnWatch.TabIndex = 7
Me.btnWatch.Text = "Start"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(870, 357)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWatch, Me.Label3, Me.txtWatcher, Me.txtFile, Me.txtPath, Me.Label2, Me.Label1})
Me.ResumeLayout(False)
End Sub
Dim WithEvents FSWatcher As New FileSystemWatcher()
Private Sub btnWatch_Click _
(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click
Dim strPathWatch As String
Dim strFileWatch As String
strPathWatch = txtPath.Text
strFileWatch = txtFile.Text
FSWatcher.Path = strPathWatch
FSWatcher.Filter = strFileWatch
FSWatcher.IncludeSubdirectories = True
FSWatcher.NotifyFilter = _
NotifyFilters.LastWrite Or NotifyFilters.FileName
FSWatcher.EnableRaisingEvents = True
txtWatcher.Text += vbCrLf + "Start:"
End Sub
Private Sub FSWatcher_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Changed
WatchMessage(e.FullPath, e.ChangeType.ToString)
End Sub
Private Sub FSWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Created
WatchMessage(e.FullPath, e.ChangeType.ToString)
End Sub
Private Sub FSWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Deleted
WatchMessage(e.FullPath, e.ChangeType.ToString)
End Sub
Private Sub FSWatcher_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles FSWatcher.Error
WatchMessage("", e.GetException.Message)
End Sub
Private Sub FSWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FSWatcher.Renamed
WatchMessage(e.FullPath, e.ChangeType.ToString)
End Sub
Private Sub WatchMessage(ByVal strpFileName As String, ByVal strpMessage As String)
Dim strMessage As String
strMessage = strpFileName + " " + strpMessage
txtWatcher.Text += vbCrLf + strMessage
End Sub
End Class