J
Jeff Grantham
I have the code below and it does what i want except for one thing and I
can't for the life of me figure it out. It makes multiple copies of the
entries in the Event Log and sends multiple emails. I would appreciate
any help in figuring out what is going on.
Thanks.
---- Begin Code ----
Imports System
Imports System.IO
Imports System.Diagnostics
Imports Microsoft.Win32
Imports System.Web.Mail
Public Class Form1
Inherits System.Windows.Forms.Form
Public watchfolder As FileSystemWatcher
Public exeWatcher As FileSystemWatcher
Public batWatcher As FileSystemWatcher
Public scrWatcher As FileSystemWatcher
Public m As New System.Web.Mail.MailMessage()
Public strComputer As String
Public strUser As String
Public strLogMsg As String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txt_watchpath As System.Windows.Forms.TextBox
Friend WithEvents btn_startwatch As System.Windows.Forms.Button
Friend WithEvents btn_stop As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txt_folderactivity As System.Windows.Forms.TextBox
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents nfi As System.Windows.Forms.NotifyIcon
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.Label1 = New System.Windows.Forms.Label()
Me.txt_watchpath = New System.Windows.Forms.TextBox()
Me.btn_startwatch = New System.Windows.Forms.Button()
Me.btn_stop = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.txt_folderactivity = New System.Windows.Forms.TextBox()
Me.ContextMenu1 = New System.Windows.Forms.ContextMenu()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.nfi = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(120, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Type Folder to Watch:"
'
'txt_watchpath
'
Me.txt_watchpath.Enabled = False
Me.txt_watchpath.Location = New System.Drawing.Point(136, 32)
Me.txt_watchpath.Name = "txt_watchpath"
Me.txt_watchpath.Size = New System.Drawing.Size(216, 20)
Me.txt_watchpath.TabIndex = 1
Me.txt_watchpath.Text = "TextBox1"
'
'btn_startwatch
'
Me.btn_startwatch.FlatStyle =
System.Windows.Forms.FlatStyle.Flat
Me.btn_startwatch.Location = New System.Drawing.Point(136, 64)
Me.btn_startwatch.Name = "btn_startwatch"
Me.btn_startwatch.Size = New System.Drawing.Size(88, 23)
Me.btn_startwatch.TabIndex = 2
Me.btn_startwatch.Text = "Start Watching"
'
'btn_stop
'
Me.btn_stop.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btn_stop.Location = New System.Drawing.Point(232, 64)
Me.btn_stop.Name = "btn_stop"
Me.btn_stop.Size = New System.Drawing.Size(88, 23)
Me.btn_stop.TabIndex = 3
Me.btn_stop.Text = "Stop Watching"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(79, 13)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Folder Activity:"
'
'txt_folderactivity
'
Me.txt_folderactivity.Location = New System.Drawing.Point(16,
104)
Me.txt_folderactivity.Multiline = True
Me.txt_folderactivity.Name = "txt_folderactivity"
Me.txt_folderactivity.Size = New System.Drawing.Size(448, 152)
Me.txt_folderactivity.TabIndex = 5
Me.txt_folderactivity.Text = "TextBox2"
'
'ContextMenu1
'
Me.ContextMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem1})
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "&Restore"
'
'MenuItem1
'
Me.MenuItem1.Index = 1
Me.MenuItem1.Text = "&Exit"
'
'nfi
'
Me.nfi.ContextMenu = Me.ContextMenu1
Me.nfi.Icon = CType(resources.GetObject("nfi.Icon"),
System.Drawing.Icon)
Me.nfi.Text = "Folder Watcher"
Me.nfi.Visible = True
'
'Button1
'
Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Button1.Location = New System.Drawing.Point(368, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 6
Me.Button1.Text = "Change"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(480, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button1, Me.txt_folderactivity, Me.Label2, Me.btn_stop,
Me.btn_startwatch, Me.txt_watchpath, Me.Label1})
Me.Icon = CType(resources.GetObject("$this.Icon"),
System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Folder Watcher"
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objKey As RegistryKey
Dim objUserKey As RegistryKey
txt_folderactivity.Text = ""
txt_watchpath.Text = "c:\"
nfi.Visible = False
objKey = Registry.LocalMachine.OpenSubKey _
("SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" _
, False)
objUserKey = Registry.LocalMachine.OpenSubKey _
("SYSTEM\CurrentControlSet\Control", False)
strComputer = Convert.ToString(objKey.GetValue _
("ComputerName", ""))
strUser = System.Environment.UserName
'Click Event copied from btn_startwatch
If txt_watchpath.Enabled = True Then
txt_watchpath.Enabled = False
End If
watchfolder = New System.IO.FileSystemWatcher()
exeWatcher = New System.IO.FileSystemWatcher()
batWatcher = New System.IO.FileSystemWatcher()
scrWatcher = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
exeWatcher.Path = txt_watchpath.Text
batWatcher.Path = txt_watchpath.Text
scrWatcher.Path = txt_watchpath.Text
watchfolder.IncludeSubdirectories = True
exeWatcher.IncludeSubdirectories = True
batWatcher.IncludeSubdirectories = True
scrWatcher.IncludeSubdirectories = True
watchfolder.Filter = "*.cmd"
exeWatcher.Filter = "*.exe"
batWatcher.Filter = "*.bat"
scrWatcher.Filter = "*.scr"
'Add a list of Filters we want to specify
'make sure you use OR for each filter as we need to all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
'add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler exeWatcher.Changed, AddressOf logchange
AddHandler batWatcher.Changed, AddressOf logchange
AddHandler scrWatcher.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler exeWatcher.Created, AddressOf logchange
AddHandler batWatcher.Created, AddressOf logchange
AddHandler scrWatcher.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler exeWatcher.Deleted, AddressOf logchange
AddHandler batWatcher.Deleted, AddressOf logchange
AddHandler scrWatcher.Deleted, AddressOf logchange
'add the rename handler as the signature is differnet
AddHandler watchfolder.Renamed, AddressOf logrename
AddHandler exeWatcher.Renamed, AddressOf logrename
AddHandler batWatcher.Renamed, AddressOf logrename
AddHandler scrWatcher.Renamed, AddressOf logrename
'set this property to true to start watching
watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True
btn_startwatch.Enabled = False
btn_stop.Enabled = True
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
nfi.Visible = True
End If
End Sub
Private Sub btn_startwatch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btn_startwatch.Click
If txt_watchpath.Enabled = True Then
txt_watchpath.Enabled = False
End If
watchfolder = New System.IO.FileSystemWatcher()
exeWatcher = New System.IO.FileSystemWatcher()
batWatcher = New System.IO.FileSystemWatcher()
scrWatcher = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
exeWatcher.Path = txt_watchpath.Text
batWatcher.Path = txt_watchpath.Text
scrWatcher.Path = txt_watchpath.Text
watchfolder.IncludeSubdirectories = True
exeWatcher.IncludeSubdirectories = True
batWatcher.IncludeSubdirectories = True
scrWatcher.IncludeSubdirectories = True
watchfolder.Filter = "*.cmd"
exeWatcher.Filter = "*.exe"
batWatcher.Filter = "*.bat"
scrWatcher.Filter = "*.scr"
'Add a list of Filters we want to specify
'make sure you use OR for each filter as we need to all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
'add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler exeWatcher.Changed, AddressOf logchange
AddHandler batWatcher.Changed, AddressOf logchange
AddHandler scrWatcher.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler exeWatcher.Created, AddressOf logchange
AddHandler batWatcher.Created, AddressOf logchange
AddHandler scrWatcher.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler exeWatcher.Deleted, AddressOf logchange
AddHandler batWatcher.Deleted, AddressOf logchange
AddHandler scrWatcher.Deleted, AddressOf logchange
'add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename
AddHandler exeWatcher.Renamed, AddressOf logrename
AddHandler batWatcher.Renamed, AddressOf logrename
AddHandler scrWatcher.Renamed, AddressOf logrename
'set this property to true to start watching
watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True
btn_startwatch.Enabled = False
btn_stop.Enabled = True
End Sub
Private Sub btn_stop_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btn_stop.Click
If txt_watchpath.Enabled = False Then
txt_watchpath.Enabled = True
End If
watchfolder.EnableRaisingEvents = False
exeWatcher.EnableRaisingEvents = False
batWatcher.EnableRaisingEvents = False
scrWatcher.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " -
File " & e.FullPath & _
" has been modified - " & Now()
ElseIf e.ChangeType = IO.WatcherChangeTypes.Created Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " -
File " & e.FullPath & _
" has been created - " & Now()
Else
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & "
- File " & e.FullPath & _
" has been deleted - " & Now()
End If
End If
SaveTextToFile(strLogMsg, "C:\Log.txt")
LognEmail(strLogMsg)
strLogMsg = ""
End Sub
Public Sub logrename(ByVal source As Object, ByVal e As
System.IO.RenamedEventArgs)
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " - File
" & e.OldName & _
" has been renamed to " & e.Name & vbCrLf
LognEmail(strLogMsg)
strLogMsg = ""
End Sub
Private Sub nfi_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles nfi.DoubleClick
Me.Visible = True
Me.WindowState = FormWindowState.Normal
nfi.Visible = False
End Sub
Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
Me.Close()
End Sub
Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Me.Visible = True
Me.WindowState = FormWindowState.Normal
nfi.Visible = False
End Sub
Private Sub Form1_MinimumSizeChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles MyBase.MinimumSizeChanged
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
nfi.Visible = True
End If
End Sub
Private Sub nfi_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles nfi.Click
End Sub
Function GetEmbeddedIcon(ByVal strName As String) As Icon
Return New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResource
Stream(strName))
End Function
Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function
Public Function SaveTextToFile(ByVal strData As String, _
ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean
Dim Contents As String
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath, True)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
Public Function LognEmail(ByVal strMessage As String)
Dim sSource As String
Dim sLog As String
Dim sEvent As String
sSource = "FolderWatcher"
sLog = "System"
sEvent = strMessage
If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If
'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning,
234)
With m
.From = strUser & "@mcglinchey.com"
.To = "(e-mail address removed)"
'.CC = "(e-mail address removed)"
.Subject = "FolderWatcher Notification"
.Body = sEvent
.Priority = MailPriority.High
End With
SmtpMail.SmtpServer = "mail.mcglinchey.com"
SmtpMail.Send(m)
strLogMsg = ""
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
txt_watchpath.Enabled = True
txt_watchpath.SelectAll()
End Sub
End Class
---- End Code ----
can't for the life of me figure it out. It makes multiple copies of the
entries in the Event Log and sends multiple emails. I would appreciate
any help in figuring out what is going on.
Thanks.
---- Begin Code ----
Imports System
Imports System.IO
Imports System.Diagnostics
Imports Microsoft.Win32
Imports System.Web.Mail
Public Class Form1
Inherits System.Windows.Forms.Form
Public watchfolder As FileSystemWatcher
Public exeWatcher As FileSystemWatcher
Public batWatcher As FileSystemWatcher
Public scrWatcher As FileSystemWatcher
Public m As New System.Web.Mail.MailMessage()
Public strComputer As String
Public strUser As String
Public strLogMsg As String
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txt_watchpath As System.Windows.Forms.TextBox
Friend WithEvents btn_startwatch As System.Windows.Forms.Button
Friend WithEvents btn_stop As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txt_folderactivity As System.Windows.Forms.TextBox
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents nfi As System.Windows.Forms.NotifyIcon
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.Label1 = New System.Windows.Forms.Label()
Me.txt_watchpath = New System.Windows.Forms.TextBox()
Me.btn_startwatch = New System.Windows.Forms.Button()
Me.btn_stop = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.txt_folderactivity = New System.Windows.Forms.TextBox()
Me.ContextMenu1 = New System.Windows.Forms.ContextMenu()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.nfi = New System.Windows.Forms.NotifyIcon(Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 32)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(120, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Type Folder to Watch:"
'
'txt_watchpath
'
Me.txt_watchpath.Enabled = False
Me.txt_watchpath.Location = New System.Drawing.Point(136, 32)
Me.txt_watchpath.Name = "txt_watchpath"
Me.txt_watchpath.Size = New System.Drawing.Size(216, 20)
Me.txt_watchpath.TabIndex = 1
Me.txt_watchpath.Text = "TextBox1"
'
'btn_startwatch
'
Me.btn_startwatch.FlatStyle =
System.Windows.Forms.FlatStyle.Flat
Me.btn_startwatch.Location = New System.Drawing.Point(136, 64)
Me.btn_startwatch.Name = "btn_startwatch"
Me.btn_startwatch.Size = New System.Drawing.Size(88, 23)
Me.btn_startwatch.TabIndex = 2
Me.btn_startwatch.Text = "Start Watching"
'
'btn_stop
'
Me.btn_stop.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btn_stop.Location = New System.Drawing.Point(232, 64)
Me.btn_stop.Name = "btn_stop"
Me.btn_stop.Size = New System.Drawing.Size(88, 23)
Me.btn_stop.TabIndex = 3
Me.btn_stop.Text = "Stop Watching"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(79, 13)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Folder Activity:"
'
'txt_folderactivity
'
Me.txt_folderactivity.Location = New System.Drawing.Point(16,
104)
Me.txt_folderactivity.Multiline = True
Me.txt_folderactivity.Name = "txt_folderactivity"
Me.txt_folderactivity.Size = New System.Drawing.Size(448, 152)
Me.txt_folderactivity.TabIndex = 5
Me.txt_folderactivity.Text = "TextBox2"
'
'ContextMenu1
'
Me.ContextMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem1})
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "&Restore"
'
'MenuItem1
'
Me.MenuItem1.Index = 1
Me.MenuItem1.Text = "&Exit"
'
'nfi
'
Me.nfi.ContextMenu = Me.ContextMenu1
Me.nfi.Icon = CType(resources.GetObject("nfi.Icon"),
System.Drawing.Icon)
Me.nfi.Text = "Folder Watcher"
Me.nfi.Visible = True
'
'Button1
'
Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Button1.Location = New System.Drawing.Point(368, 32)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 6
Me.Button1.Text = "Change"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(480, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control()
{Me.Button1, Me.txt_folderactivity, Me.Label2, Me.btn_stop,
Me.btn_startwatch, Me.txt_watchpath, Me.Label1})
Me.Icon = CType(resources.GetObject("$this.Icon"),
System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Folder Watcher"
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim objKey As RegistryKey
Dim objUserKey As RegistryKey
txt_folderactivity.Text = ""
txt_watchpath.Text = "c:\"
nfi.Visible = False
objKey = Registry.LocalMachine.OpenSubKey _
("SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" _
, False)
objUserKey = Registry.LocalMachine.OpenSubKey _
("SYSTEM\CurrentControlSet\Control", False)
strComputer = Convert.ToString(objKey.GetValue _
("ComputerName", ""))
strUser = System.Environment.UserName
'Click Event copied from btn_startwatch
If txt_watchpath.Enabled = True Then
txt_watchpath.Enabled = False
End If
watchfolder = New System.IO.FileSystemWatcher()
exeWatcher = New System.IO.FileSystemWatcher()
batWatcher = New System.IO.FileSystemWatcher()
scrWatcher = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
exeWatcher.Path = txt_watchpath.Text
batWatcher.Path = txt_watchpath.Text
scrWatcher.Path = txt_watchpath.Text
watchfolder.IncludeSubdirectories = True
exeWatcher.IncludeSubdirectories = True
batWatcher.IncludeSubdirectories = True
scrWatcher.IncludeSubdirectories = True
watchfolder.Filter = "*.cmd"
exeWatcher.Filter = "*.exe"
batWatcher.Filter = "*.bat"
scrWatcher.Filter = "*.scr"
'Add a list of Filters we want to specify
'make sure you use OR for each filter as we need to all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
'add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler exeWatcher.Changed, AddressOf logchange
AddHandler batWatcher.Changed, AddressOf logchange
AddHandler scrWatcher.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler exeWatcher.Created, AddressOf logchange
AddHandler batWatcher.Created, AddressOf logchange
AddHandler scrWatcher.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler exeWatcher.Deleted, AddressOf logchange
AddHandler batWatcher.Deleted, AddressOf logchange
AddHandler scrWatcher.Deleted, AddressOf logchange
'add the rename handler as the signature is differnet
AddHandler watchfolder.Renamed, AddressOf logrename
AddHandler exeWatcher.Renamed, AddressOf logrename
AddHandler batWatcher.Renamed, AddressOf logrename
AddHandler scrWatcher.Renamed, AddressOf logrename
'set this property to true to start watching
watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True
btn_startwatch.Enabled = False
btn_stop.Enabled = True
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
nfi.Visible = True
End If
End Sub
Private Sub btn_startwatch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btn_startwatch.Click
If txt_watchpath.Enabled = True Then
txt_watchpath.Enabled = False
End If
watchfolder = New System.IO.FileSystemWatcher()
exeWatcher = New System.IO.FileSystemWatcher()
batWatcher = New System.IO.FileSystemWatcher()
scrWatcher = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text
exeWatcher.Path = txt_watchpath.Text
batWatcher.Path = txt_watchpath.Text
scrWatcher.Path = txt_watchpath.Text
watchfolder.IncludeSubdirectories = True
exeWatcher.IncludeSubdirectories = True
batWatcher.IncludeSubdirectories = True
scrWatcher.IncludeSubdirectories = True
watchfolder.Filter = "*.cmd"
exeWatcher.Filter = "*.exe"
batWatcher.Filter = "*.bat"
scrWatcher.Filter = "*.scr"
'Add a list of Filters we want to specify
'make sure you use OR for each filter as we need to all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
exeWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
batWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
scrWatcher.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
'add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler exeWatcher.Changed, AddressOf logchange
AddHandler batWatcher.Changed, AddressOf logchange
AddHandler scrWatcher.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler exeWatcher.Created, AddressOf logchange
AddHandler batWatcher.Created, AddressOf logchange
AddHandler scrWatcher.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler exeWatcher.Deleted, AddressOf logchange
AddHandler batWatcher.Deleted, AddressOf logchange
AddHandler scrWatcher.Deleted, AddressOf logchange
'add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename
AddHandler exeWatcher.Renamed, AddressOf logrename
AddHandler batWatcher.Renamed, AddressOf logrename
AddHandler scrWatcher.Renamed, AddressOf logrename
'set this property to true to start watching
watchfolder.EnableRaisingEvents = True
exeWatcher.EnableRaisingEvents = True
batWatcher.EnableRaisingEvents = True
scrWatcher.EnableRaisingEvents = True
btn_startwatch.Enabled = False
btn_stop.Enabled = True
End Sub
Private Sub btn_stop_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btn_stop.Click
If txt_watchpath.Enabled = False Then
txt_watchpath.Enabled = True
End If
watchfolder.EnableRaisingEvents = False
exeWatcher.EnableRaisingEvents = False
batWatcher.EnableRaisingEvents = False
scrWatcher.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " -
File " & e.FullPath & _
" has been modified - " & Now()
ElseIf e.ChangeType = IO.WatcherChangeTypes.Created Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " -
File " & e.FullPath & _
" has been created - " & Now()
Else
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
strLogMsg &= vbCrLf & strUser & " on " & strComputer & "
- File " & e.FullPath & _
" has been deleted - " & Now()
End If
End If
SaveTextToFile(strLogMsg, "C:\Log.txt")
LognEmail(strLogMsg)
strLogMsg = ""
End Sub
Public Sub logrename(ByVal source As Object, ByVal e As
System.IO.RenamedEventArgs)
strLogMsg &= vbCrLf & strUser & " on " & strComputer & " - File
" & e.OldName & _
" has been renamed to " & e.Name & vbCrLf
LognEmail(strLogMsg)
strLogMsg = ""
End Sub
Private Sub nfi_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles nfi.DoubleClick
Me.Visible = True
Me.WindowState = FormWindowState.Normal
nfi.Visible = False
End Sub
Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
Me.Close()
End Sub
Private Sub MenuItem2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Me.Visible = True
Me.WindowState = FormWindowState.Normal
nfi.Visible = False
End Sub
Private Sub Form1_MinimumSizeChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles MyBase.MinimumSizeChanged
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.Visible = False
nfi.Visible = True
End If
End Sub
Private Sub nfi_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles nfi.Click
End Sub
Function GetEmbeddedIcon(ByVal strName As String) As Icon
Return New
Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResource
Stream(strName))
End Function
Public Function GetFileContents(ByVal FullPath As String, _
Optional ByRef ErrInfo As String = "") As String
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader(FullPath)
strContents = objReader.ReadToEnd()
objReader.Close()
Return strContents
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
End Function
Public Function SaveTextToFile(ByVal strData As String, _
ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean
Dim Contents As String
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath, True)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
Public Function LognEmail(ByVal strMessage As String)
Dim sSource As String
Dim sLog As String
Dim sEvent As String
sSource = "FolderWatcher"
sLog = "System"
sEvent = strMessage
If Not EventLog.SourceExists(sSource) Then
EventLog.CreateEventSource(sSource, sLog)
End If
'EventLog.WriteEntry(sSource, sEvent)
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning,
234)
With m
.From = strUser & "@mcglinchey.com"
.To = "(e-mail address removed)"
'.CC = "(e-mail address removed)"
.Subject = "FolderWatcher Notification"
.Body = sEvent
.Priority = MailPriority.High
End With
SmtpMail.SmtpServer = "mail.mcglinchey.com"
SmtpMail.Send(m)
strLogMsg = ""
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
txt_watchpath.Enabled = True
txt_watchpath.SelectAll()
End Sub
End Class
---- End Code ----