Folder changes monitor made in Visual Basic 2010 does not write changes correctly

前端 未结 2 809
旧时难觅i
旧时难觅i 2021-01-17 02:49

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

相关标签:
2条回答
  • 2021-01-17 03:05

    Now the program in working! Thank you MPelletier and Plutonix for the amazing help! Here is the complete 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()
        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
    
    0 讨论(0)
  • 2021-01-17 03:15

    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

    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
    
    0 讨论(0)
提交回复
热议问题