记录 [英] Logging

查看:61
本文介绍了记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,除了一件事我做了我想做的事情,而且我不能为我的生活找到它。它会在事件日志中制作

条目的多个副本,并发送多封电子邮件。我很感激

帮助搞清楚发生了什么。


谢谢。

----开始代码 - ---

进口系统

进口System.IO

进口System.Diagnostics

进口Microsoft.Win32

导入System.Web.Mail

公共类Form1

继承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窗体设计器生成的代码


Public Sub New()

MyBase.New()


' 'Windows窗体设计器需要此调用。

InitializeComponent()

''在InitializeComponent()调用后添加任何初始化
< br $>
结束子


''表格覆盖处理以清理组件列表。

受保护的重载覆盖子处理(ByVal处理为

布尔值)

如果处理那么

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase.Dispose(处置)

End Sub


''Windows窗体设计器要求

私有组件As System.ComponentModel.IContainer


''注意:以下过程是Windows窗体要求

设计器

''可以使用Windows窗体设计器修改它。

''不要使用代码编辑器修改它。

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 =新的

System.Resources.ResourceManager(GetType(Form1))

Me.Label1 =新的System.Windows.Forms.Label()

Me.txt_watchpath = New System.Windows.Forms.TextBox()

Me.btn_startwatch = New System.Windows.Forms.Button()

Me.btn_stop =新系统.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 ="输入要观看的文件夹:"

''

''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 ="开始看着

'

''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 ="停止观看>

''

''标签2

''

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 ="文件夹活动:"

''

''txt_folderactivity

'

Me.txt_folderactivity.Location =新的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.CenterScree n

Me.Text =" Folder Watcher"
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized

Me.ResumeLayout(假)


结束子


#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 \ Co mputerName" _

,错误)


objUserKey = Registry.LocalMachine.OpenSubKey _

(" SYSTEM \ CurrentControlSet \ Control" ;,错误)


strComputer = Convert.ToString(objKey.GetValue _

(" ComputerName",""))


strUser = System.Environment.UserName


''点击从btn_startwatch复制的事件

如果txt_watchpath.Enabled = True则

txt_watchpath.Enabled = False

结束如果


watchfolder = New System.IO.FileSystemWatcher()

exeWatcher = New System.IO.FileSystemWatcher()

batWatcher = New System.IO.FileSystemWatcher()

scrWatcher = New System.IO.FileSystemWatcher()


''这是我们要监控的路径

watchfolder.Path = txt_watchpath.Text

exeWatcher.Path = txt_watchpath.Text

batWatcher.Path = txt_watchpath.Text

scrWatcher.Path = txt_watchpath.Tex t $ / $

watchfolder.IncludeSubdirectories = True

exeWatcher.IncludeSubdirectories = True

batWatcher.IncludeSubdirectories = True

scrWatcher.IncludeSubdirectories = True


watchfolder.Filter =" * .cmd"

exeWatcher.Filter =" * .exe"

batWatcher.Filter =" * .bat"

scrWatcher.Filter =" * .scr"


''添加清单我们想要指定的过滤器

''确保为每个过滤器使用OR,因为我们需要所有这些过滤器。

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName

exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName


watchfolder.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


exeWatcher.NotifyFilter = watchfo lder.NotifyFilter或_

IO.NotifyFilters.FileName


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters .FileName


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


watchfolder。 NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO .NotifyFilters.Attributes


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


''为每个活动添加处理程序

AddHandler watchfolder.Changed,AddressOf logchange

AddHandler exeWatcher.Changed,AddressOf logchange

AddHandler batWatcher.Changed,AddressOf logchange

AddHandler sc rWatcher.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。删除,AddressOf logchange

AddHandler batWatcher.Deleted,AddressOf logchange

AddHandler scrWatcher.Deleted,AddressOf logchange


''添加重命名处理程序,因为签名是不同的

AddHandler watchfolder.Renamed,AddressOf logrename

AddHandler exeWatcher.Renamed,AddressOf logrename

AddHandler batWatcher.Renamed, AddressOf logrename

AddHandler scrWatcher.Renamed,AddressOf logrename


''将此属性设置为true以开始观看

watchfolder.EnableRaisingEvents =真的

exeWatcher.EnableRaisingEvents = True

batWatcher.EnableRaisingEvents = True

scrWatcher.EnableRaisingEvents = True


btn_startwatch.Enabled = False


btn_stop.Enabled = True


如果Me.WindowState = FormWindowState.Minimized那么

我。 Visible = False

nfi.Visible = True

结束如果


End Sub


Private Sub btn_startwatch_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理btn_startwatch.Click


如果txt_watchpath.Enabled = True则

txt_watchpath.Enabled = False

结束如果


watchfolder = New System.IO.FileSystemWatcher()

exeWatcher = New System.IO.FileSystemWatcher()

batWatcher = New System.IO.FileSystemWatcher()

scrWatcher = New System.IO.FileSystemWatcher()


''这是我们要监控的路径

watchfolder.Path = txt_wat chpath.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"


''添加我们的过滤器列表想要指定

''确保你为每个过滤器使用OR,因为我们需要所有这些

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName

exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
<无线电通信/>
watchfolder.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_ < br $>
IO.NotifyFilters.FileName


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _

IO.NotifyFilters.Attributes


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


scrWatcher.NotifyFilter = watchfolder。 NotifyFilter或_

IO.NotifyFilters.Attributes


''为每个事件添加处理程序

AddHandler watchfolder.Changed,AddressOf logc hange

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


''添加重命名处理程序,因为签名不同

AddHandler watchfolder.Renamed,AddressOf logrename

AddHandler exeWatcher.Renamed,AddressOf logrename

AddHandler batWatcher.Renamed,AddressOf logrename

AddHandler scrWatcher .Renamed,AddressOf logrename


''将此属性设置为true以开始观看

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)处理btn_stop.Click


如果txt_watchpath.Enabled = False那么

txt_watchpath.Enabled = True

结束如果

watchfolder.EnableRaisingEvents = False

exeWatcher.EnableRaisingEvents = False

batWatcher.EnableRaisingEvents = False

scrWatcher.EnableRaisingEvents = False


btn_startwatch.Enabled = True

btn_stop.Enabled = False


End Sub


私人子l ogchange(ByVal source As Object,ByVal e As

System.IO.FileSystemEventArgs)


如果e.ChangeType = IO.WatcherChangeTypes.Changed那么


strLogMsg& = vbCrLf& strUser& "在 &安培;计算机与计算机" -

档案 &安培; e.FullPath& _

"已被修改 - &安培;现在()


ElseIf e.ChangeType = IO.WatcherChangeTypes.Created那么


strLogMsg& = vbCrLf& strUser& "在 &安培;计算机与计算机" -

档案 &安培; e.FullPath& _

"已被创建 - &安培;现在()


否则


如果e.ChangeType = IO.WatcherChangeTypes.Deleted那么


strLogMsg& = vbCrLf& strUser& "在 &安培;计算机与计算机

- 文件 &安培; e.FullPath& _

"已被删除 - &安培;现在()


结束如果

结束如果


SaveTextToFile(strLogMsg," C:\ Log) .txt


LognEmail(strLogMsg)


strLogMsg =""


结束Sub


Public Sub logrename(ByVal source As Object,ByVal e As

System.IO.RenamedEventArgs)


strLogMsg& = vbCrLf& strUser& "在 &安培;计算机与计算机" - 文件

" &安培; e.OldName& _

"已更名为 &安培; e.Name& vbCrLf


LognEmail(strLogMsg)


strLogMsg =""


End Sub


Private Sub nfi_DoubleClick(ByVal sender As Object,ByVal e As

System.EventArgs)处理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


私人子MenuItem2_Click(ByVal发送者为对象,ByVal e As

System.EventArgs)处理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)处理MyBase.Minimu mSizeChanged


End Sub


Private Sub Form1_Resize(ByVal sender As Object,ByVal e As

System.EventArgs )处理MyBase.Resize


如果Me.WindowState = FormWindowState.Minimized那么

Me.Visible = False

nfi.Visible = True

结束如果


结束子


私有子nfi_Click(ByVal发送者为对象,ByVal e As

System.EventArgs)处理nfi.Click


End Sub


函数GetEmbeddedIcon(ByVal strName As String)作为图标


返回新的

图标(System.Reflection.Assembly.GetExecutingAssemb ly.GetManifestResource

Stream(strName))


结束函数


公共函数GetFileContents(ByVal FullPath As String,_

可选ByRef ErrInfo As String =" ;")As String


Dim strContents As String

Dim objReader As StreamReader

尝试


objReader = New StreamReader(FullPath)

strContents = objReader.ReadToEnd()

objReader.Close()

返回strContents

Catch Ex As Exception>
ErrInfo = Ex.Message

结束尝试

结束功能


公共函数SaveTextToFile(ByVal strData As String,_

ByVal FullPath As String,_

可选ByVal ErrInfo As String ="")As Boolean


昏暗内容为字符串

Dim bAns As Boolean = False

Dim objReader As StreamWriter

尝试

objReader = New StreamWriter(FullPath,True)

objReader.Write(strData)

objReader.Close()

bAns = True

Catch Ex As Exception>
ErrInfo = Ex.Message


结束尝试

返回bAns

结束功能

公共功能LognEmail(ByVal strMessage As String)


Dim sSource As String

Dim sLog As Str ing $ / $
Dim sEvent As String


sSource =" FolderWatcher"

sLog =" System"

sEvent = strMessage


如果不是EventLog.SourceExists(sSource)那么

EventLog.CreateEventSource(sSource,sLog)

结束如果


''EventLog.WriteEntry(sSource,sEvent)

EventLog.WriteEntry(sSource,sEvent,EventLogEntryType.Warning,

234)


随着m

.From = strUser& " @ mcglinchey.com"

.To =" jg ******* @ mcglinchey.com"

''。CC =" jr * **@mcglinchey.com"

.Subject =" FolderWatcher Notification"

.Body = sEvent

.Priority = MailPriority.High

结束


SmtpMail.SmtpServer =" mail.mcglinchey.com"

SmtpMail.Send(m)


strLogMsg =""


结束功能

Private Sub Button1_Click(ByVal sender As System。对象,ByVal e As

System.EventArgs)处理Button1.Click


txt_watchpath.Enabled = True

txt_watchpath.SelectAll( )


结束次级

结束班级


----结束代码----


***通过开发人员指南 http://www.developersdex.com发送 ***

不要只是参加USENET ......获得奖励!

解决方案

this.Icon"),

System.Drawing.Icon)

Me.MaximizeBox = False

Me.Name =" Form1"

Me.StartPosition =

System.Windows.Forms.FormStartPosition.CenterScree n

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作为

System.EventArgs)处理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 \ ComuterNa ME" _

,错误)


objUserKey = Registry.LocalMachine.OpenSubKey _

(" SYSTEM \ CurrentControlSet \ Control" ;,错误)


strComputer = Convert.ToString(objKey.GetValue _

(" ComputerName",""))


strUser = System.Environment.UserName


''点击从btn_startwatch复制的事件

如果txt_watchpath.Enabled = True则

txt_watchpath.Enabled = False

结束如果


watchfolder = New System.IO.FileSystemWatcher()

exeWatcher = New System.IO.FileSystemWatcher()

batWatcher = New System.IO.FileSystemWatcher()

scrWatcher = New System.IO.FileSystemWatcher()


''这是我们要监控的路径

watchfolder.Path = txt_watchpath.Text

exeWatcher.Path = txt_watchpath.Text

batWatcher.Path = txt_watchpath.Text

scrWatcher.Path = txt_watchpath.Tex t $ / $

watchfolder.IncludeSubdirectories = True

exeWatcher.IncludeSubdirectories = True

batWatcher.IncludeSubdirectories = True

scrWatcher.IncludeSubdirectories = True


watchfolder.Filter =" * .cmd"

exeWatcher.Filter =" * .exe"

batWatcher.Filter =" * .bat"

scrWatcher.Filter =" * .scr"


''添加清单我们想要指定的过滤器

''确保为每个过滤器使用OR,因为我们需要所有这些过滤器。

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName

exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName


watchfolder.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


exeWatcher.NotifyFilter = watchfo lder.NotifyFilter或_

IO.NotifyFilters.FileName


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters .FileName


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


watchfolder。 NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO .NotifyFilters.Attributes


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


''为每个活动添加处理程序

AddHandler watchfolder.Changed,AddressOf logchange

AddHandler exeWatcher.Changed,AddressOf logchange

AddHandler batWatcher.Changed,AddressOf logchange

AddHandler sc rWatcher.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。删除,AddressOf logchange

AddHandler batWatcher.Deleted,AddressOf logchange

AddHandler scrWatcher.Deleted,AddressOf logchange


''添加重命名处理程序,因为签名是不同的

AddHandler watchfolder.Renamed,AddressOf logrename

AddHandler exeWatcher.Renamed,AddressOf logrename

AddHandler batWatcher.Renamed, AddressOf logrename

AddHandler scrWatcher.Renamed,AddressOf logrename


''将此属性设置为true以开始观看

watchfolder.EnableRaisingEvents =真的

exeWatcher.EnableRaisingEvents = True

batWatcher.EnableRaisingEvents = True

scrWatcher.EnableRaisingEvents = True


btn_startwatch.Enabled = False


btn_stop.Enabled = True


如果Me.WindowState = FormWindowState.Minimized那么

我。 Visible = False

nfi.Visible = True

结束如果


End Sub


Private Sub btn_startwatch_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理btn_startwatch.Click


如果txt_watchpath.Enabled = True则

txt_watchpath.Enabled = False

结束如果


watchfolder = New System.IO.FileSystemWatcher()

exeWatcher = New System.IO.FileSystemWatcher()

batWatcher = New System.IO.FileSystemWatcher()

scrWatcher = New System.IO.FileSystemWatcher()


''这是我们要监控的路径

watchfolder.Path = txt_wat chpath.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"


''添加我们的过滤器列表想要指定

''确保你为每个过滤器使用OR,因为我们需要所有这些

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName

exeWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

batWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName

scrWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName
<无线电通信/>
watchfolder.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_ < br $>
IO.NotifyFilters.FileName


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


scrWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.FileName


watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _

IO.NotifyFilters.Attributes


exeWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


batWatcher.NotifyFilter = watchfolder.NotifyFilter或_

IO.NotifyFilters.Attributes


scrWatcher.NotifyFilter = watchfolder。 NotifyFilter或_

IO.NotifyFilters.Attributes


''为每个事件添加处理程序

AddHandler watchfolder.Changed,AddressOf logc hange

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
$ b$b AddHandler scrWatcher.Created, AddressOf logchange


AddHandler watchfolder.Deleted, AddressOf logchange

AddHandler exeWatcher.Deleted, AddressOf logchange
$b $b 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 l ogchange(ByVal source As Object, ByVal e As

System.IO.FileSystemEventArgs)


If e.ChangeType = IO.WatcherChangeTypes.Changed Then


strLogMsg &= vbCrLf & strUser & "在 &安培;计算机与计算机" -

File " &安培; e.FullPath & _

" has been modified - " &安培; Now()


ElseIf e.ChangeType = IO.WatcherChangeTypes.Created Then


strLogMsg &= vbCrLf & strUser & "在 &安培;计算机与计算机" -

File " &安培; e.FullPath & _

" has been created - " &安培; Now()


Else


If e.ChangeType = IO.WatcherChangeTypes.Deleted Then


strLogMsg &= vbCrLf & strUser & "在 &安培;计算机与计算机"

- 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 & "在 &安培;计算机与计算机" - 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.Minimu mSizeChanged


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.GetExecutingAssemb ly.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 Str ing

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 = "jg*******@mcglinchey.com"

’’.CC = "jr***@mcglinchey.com"

.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 ----


*** Sent via Developersdex http://www.developersdex.com ***

Don’’t just participate in USENET...get rewarded for it!



"Jeff Grantham" <jg*******@mcglinchey.com> wrote in message


Simply too much code and no pointers to the bit that’’s wrong. No idea what

you’’ve already tried. No explanation of what you think might be wrong.


Sometimes "Less is more" :-)


Richie


I would like to use the filesystemwatcher to monitor an ftp site and download a file

when it changes. I have all the code working except specifying tha path to watch.

While I have no problems watching a physical path c:\ on my machine I do not

know how to translate the ftp site’’s path into something acceptable to the path of the filesystemwatcher.

e.g. What does 127.0.0.1/hirsi in unc?


Is there a utility out there similar to mappath that will get me the directory name to watch?


Thanks


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.CenterScree n
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\Co mputerName" _
, 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.GetExecutingAssemb ly.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 = "jg*******@mcglinchey.com"
''.CC = "jr***@mcglinchey.com"
.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 ----

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

解决方案

this.Icon"),
System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n
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\Co mputerName" _
, 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.GetExecutingAssemb ly.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 = "jg*******@mcglinchey.com"
''.CC = "jr***@mcglinchey.com"
.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 ----

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



"Jeff Grantham" <jg*******@mcglinchey.com> wrote in message

Simply too much code and no pointers to the bit that''s wrong. No idea what
you''ve already tried. No explanation of what you think might be wrong.

Sometimes "Less is more" :-)

Richie


I would like to use the filesystemwatcher to monitor an ftp site and download a file
when it changes. I have all the code working except specifying tha path to watch.
While I have no problems watching a physical path c:\ on my machine I do not
know how to translate the ftp site''s path into something acceptable to the path of the filesystemwatcher.
e.g. What does 127.0.0.1/hirsi in unc?

Is there a utility out there similar to mappath that will get me the directory name to watch?

Thanks


这篇关于记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆