2013-10-20 109 views
0

我在Visual Basic 2010中製作了一個程序,它監視指定的文件夾並檢查每個修改文件的md5代碼,當md5代碼等於md5代碼我在代碼中指定,程序應該顯示Form2(Form2.Show),但程序崩潰。如果文件在程序監視的文件夾中發生改變,如果我例如。嘗試刪除該文件,它說該文件被vshost32.exe使用,並且在關閉該程序之前無法刪除。有人可以幫助我嗎?下面是代碼:監視文件中md5代碼的文件夾監視器只會崩潰

Imports System.IO 
Imports System.Diagnostics 
Imports System.Text 
Imports System.Security.Cryptography 
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() 
    Dim md5code As String 



    Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider 
    Dim f As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192) 
    f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192) 
    md5.ComputeHash(f) 
    Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject") 
    Dim objFile = ObjFSO.GetFile(e.FullPath) 

    Dim hash As Byte() = md5.Hash 
    Dim buff As StringBuilder = New StringBuilder 
    Dim hashByte As Byte 
    For Each hashByte In hash 
     buff.Append(String.Format("{0:X1}", hashByte)) 
    Next 
    md5code = buff.ToString() 
    If md5code = "(The md5 code that I will add later)" Then 
     Form2.Show() 
    End If 
End Sub 
Public Sub logrename(ByVal source As Object, ByVal e As _ 
         System.IO.RenamedEventArgs) 
    Select Case 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 
End Class 
+0

我強烈建議將MD5相關代碼移到自己的程序中。 – Plutonix

回答

0

假設名爲Form2一個窗體類,你的代碼應該是:

dim frm2 As New Form2 
frm2.Show 

形式僅僅是課程和必須被實例化使用。它似乎也沒有關閉用於讀取文件的文件流和/或釋放可能導致其他錯誤的文件上創建的對象。