0

我在 Visual Basic 2010 中制作了一个程序,它监视指定的文件夹并检查每个修改文件的 md5 代码,当 md5 代码等于我在代码中指定的 md5 代码时,程序应该显示 Form2 (Form2.显示),但程序反而崩溃了。如果程序正在监视的文件夹中的文件发生更改,例如我。尝试删除该文件,它说该文件由 vshost32.exe 使用,并且在我关闭程序之前无法删除。有人可以帮我吗?这是代码:

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
4

1 回答 1

0

假设一个名为 的表单类Form2,您的代码应该是:

dim frm2 As New Form2
frm2.Show

表单只是类,必须实例化才能使用。您似乎还没有关闭用于读取文件的文件流和/或释放在文件上创建的对象,这可能是其他错误的原因。

于 2013-10-20T18:42:59.200 回答