Visual Basic 2010中所做的文件夹更改监视器无法正确写入更改 [英] Folder changes monitor made in Visual Basic 2010 does not write changes correctly

查看:97
本文介绍了Visual Basic 2010中所做的文件夹更改监视器无法正确写入更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Basic 2010中制作了一个程序,该程序监视并记录文件夹中的更改,例如.什么时候删除文件,什么时候重命名文件,什么时候创建文件以及哪些文件,但这是一个问题.我已经编写了在进行另一次更改时换行的代码,当进行了更改时,它会将其写到名为log.txt的文件中,但是日志仅看起来像文件log.txt已被修改"因为该程序在将更改写入日志时会更改log.txt以记下日志,但是奇怪的是,即使我将程序删除文档中的所有内容并写入"File log..txt已被修改",在编写之前已经在代码中编写了新的一行.有人可以帮我解决这个问题吗?这是代码:

I've made a program in Visual Basic 2010, that monitors and write down changes in a folder eg. when a file deletes, when a file renames, when a file creates and which files, but it's a problem. I've writed the code to make a new line when another change is made, when a change is made, it writes it down to a file named log.txt, but the log only looks like "File log.txt has been modified" because the program, when it write changes to the log, it changes the log.txt to write down the log, but the strange is, it deletes everything in the document and writes "File log..txt has been modified" even if I have writed in the code to make a new line before writing. Can someone help me with this problem? Here's the code:

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

推荐答案

打开流写入器时,您并没有告诉它追加内容,因此它会覆盖:

When you open your streamwriter, you are not telling it to append, so it overwrites:

Dim writer As New IO.StreamWriter("log.txt", True)

此外,您无需为每个活动创建新的流:

Also, you dont need a new stream for each activity:

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()

..您还可以将视频流保持打开状态,直到观看者结束

..you could also leave the stream open until the watcher ends

您可能应该免除log.txt的日志记录更改,因此请测试e.FullPath:

You probably should exempt logging changes to log.txt, so test e.FullPath:

 If System.Io.Path.GetFileName(e.FullPath).ToLower = "log.text" Then Exit Sub

这篇关于Visual Basic 2010中所做的文件夹更改监视器无法正确写入更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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