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

前端 未结 2 831
旧时难觅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: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
    

提交回复
热议问题