我在Visual Basic 2010中创建了一个程序,用于监视和记录文件夹中的更改,例如。当文件删除,文件重命名时,文件创建时和文件,但这是一个问题。当我做了另一个更改时,我已经编写代码来创建一个新行,当进行更改时,它将其写入名为log.txt的文件,但日志只显示为“文件log.txt已被修改”因为程序在写入日志更改时,会更改log.txt来记下日志,但奇怪的是,它删除了文档中的所有内容并写入“文件日志.txt已被修改”,即使我在编写之前已经在代码中编写了一个新行。有人可以帮我解决这个问题吗?这是代码:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
watchfolder = New System.IO.FileSystemWatcher()
'this is the path we want to monitor
watchfolder.Path = TextBox1.Text
'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
' add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
' add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename
'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True
Button1.Enabled = False
Button2.Enabled = True
'End of code for btn_start_click
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
Dim writer As New IO.StreamWriter("log.txt")
writer.WriteLine(Chr(13) & "File" + " " + e.FullPath + " " + "has been modified")
writer.Close()
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
Dim writer As New IO.StreamWriter("log.txt")
writer.WriteLine(Chr(13) & "File" + " " + e.FullPath + " " + "has been created")
writer.Close()
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
Dim writer As New IO.StreamWriter("log.txt")
writer.WriteLine(Chr(13) & "Filde" + " " + e.FullPath + " " + "has been deleted")
writer.Close()
End If
End Sub
Public Sub logrename(ByVal source As Object, ByVal e As _
System.IO.RenamedEventArgs)
Dim writer As New IO.StreamWriter("log.txt")
writer.WriteLine(Chr(13) & "File" + " " + e.FullPath + "has been renamed to" + " " + e.Name)
writer.Close()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' Stop watching the folder
watchfolder.EnableRaisingEvents = False
Button1.Enabled = True
Button2.Enabled = False
End Sub
End Class
答案 0 :(得分:1)
当您打开您的编写器时,您没有告诉它要追加,所以它会覆盖:
Dim writer As New IO.StreamWriter("log.txt", True)
此外,您不需要为每项活动添加新流:
Dim msg as string= Environment.NewLine & "File " & e.FullPath & " "
Select case e.ChangeType
case IO.WatcherChangeTypes.Created
msg &= "has been created"
case IO.WatcherChangeTypes.Deleted
msg &= "has been deleted"
...etc
End Select
Dim writer As New IO.StreamWriter("log.txt", True)
writer.WriteLine(msg)
writer.Close()
..你也可以让流开放直到观察者结束
您可能应该将日志记录更改免除为log.txt,因此请测试e.FullPath
:
If System.Io.Path.GetFileName(e.FullPath).ToLower = "log.text" Then Exit Sub
答案 1 :(得分:0)
现在该计划正在进行中!感谢MPelletier和Plutonix提供的惊人帮助!这是完整的代码:
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
watchfolder = New System.IO.FileSystemWatcher()
watchfolder.IncludeSubdirectories = True
watchfolder.Path = TextBox1.Text
watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange
AddHandler watchfolder.Renamed, AddressOf logrename
watchfolder.EnableRaisingEvents = True
Button1.Enabled = False
Button2.Enabled = True
End Sub
Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If System.IO.Path.GetFileName(e.FullPath).ToLower = "log.txt" Then Exit Sub
Dim msg As String = Environment.NewLine & "File " & e.FullPath & " "
Select Case e.ChangeType
Case IO.WatcherChangeTypes.Created
msg &= "has been created" + " " + "Time:" + " " + Format(TimeOfDay)
Case IO.WatcherChangeTypes.Deleted
msg &= "has been deleted" + " " + "Time:" + " " + Format(TimeOfDay)
Case IO.WatcherChangeTypes.Changed
msg &= "has been modified" + " " + "Time:" + " " + Format(TimeOfDay)
End Select
Dim writer As New IO.StreamWriter("log.txt", True)
writer.WriteLine(msg)
writer.Close()
End Sub
Public Sub logrename(ByVal source As Object, ByVal e As _
System.IO.RenamedEventArgs)
Select e.ChangeType
Case IO.WatcherChangeTypes.Created
Exit Sub
Case IO.WatcherChangeTypes.Changed
Exit Sub
Case IO.WatcherChangeTypes.Deleted
Exit Sub
Case Else
Dim msgrn As String = Environment.NewLine & "File " + e.OldName + " "
msgrn &= "has been renamed to" + " " + e.Name + " " + "Time:" + " " + Format(TimeOfDay)
Dim writer As New IO.StreamWriter("log.txt", True)
writer.WriteLine(msgrn)
writer.Close()
End Select
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
watchfolder.EnableRaisingEvents = False
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Me.Hide()
MsgBox("To close it later, don't open the program again, press CTRL+ALT+DELETE and press Start Task Manager or something like that, and go to processes and kill FolderMonitor.exe or what you have named the file", 0 + 64, "FolderMonitor")
End Sub
End Class