编辑:我试图更改代码,例如。而是使用不同的潜艇。但是现在,当某些事情发生变化时,程序只会崩溃。我已经禁用了日志记录、设置断点等,但程序并没有持续多久。这是 Visual Basic 日志中的错误消息(每次程序崩溃时都会出现):
System.Windows.Forms.dll 中出现“System.InvalidOperationException”类型的第一次机会异常
这是代码(如果您想知道 ExecProtectCompareModule 和 ExecProtect 是什么,我已经进行了一些进程监视器测试):
Imports System.IO
Imports System.Diagnostics
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Dim processList As String
Dim processList2 As String
Public watchfolder As FileSystemWatcher
Dim log As String
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)
' 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
'log &= msg
'log &= Chr(13)
'Dim writer As New IO.StreamWriter("log.txt", True)
'writer.WriteLine(msg)
'writer.Close()
Label6.Text = e.FullPath
md5checkdelay.Start()
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)
' log &= msgrn
' log &= Chr(13)
'Dim writer As New IO.StreamWriter("log.txt", True)
'writer.WriteLine(msgrn)
'writer.Close()
Label5.Text = e.FullPath
md5checkdelay.Start()
End Select
End Sub
Sub md5check()
Dim md5code As String
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(Label5.Text, 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 = "D41D8CD98F0B24E980998ECF8427E" Then
Dim frm2 As New Form2
frm2.Show()
f.Close()
Else
f.Close()
End If
End Sub
Sub md5check2()
Dim md5code As String
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(Label5.Text, 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 = "D41D8CD98F0B24E980998ECF8427E" Then
Dim frm2 As New Form2
frm2.Show()
f.Close()
Else
f.Close()
End If
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
Dim frm2 As New Form2
frm2.ShowDialog()
End Sub
Private Sub ExecProtect_Tick(sender As System.Object, e As System.EventArgs) Handles ExecProtectMonitorModule.Tick
For Each p As Process In Process.GetProcesses()
processList = processList & " " & p.ProcessName & vbNewLine
Next
Label3.Text = processList
End Sub
Private Sub ExecProtectCompareModule_Tick(sender As System.Object, e As System.EventArgs) Handles ExecProtectCompareModule.Tick
If Not Label2.Text = Label3.Text Then
MsgBox("New process started!", 0 + 64)
processList2refresh()
End If
End Sub
Sub processList2refresh()
For Each p As Process In Process.GetProcesses()
processList2 = processList2 & " " & p.ProcessName & vbNewLine
Exit Sub
Next
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For Each p As Process In Process.GetProcesses()
processList2 = processList2 & " " & p.ProcessName & vbNewLine
Next
Label2.Text = processList2
End Sub
Private Sub md5checkdelay_Tick(sender As System.Object, e As System.EventArgs) Handles md5checkdelay.Tick
'The timer that adds 1 second delay before it checks the md5code after the file(s) is changed
Label4.Text = Label4.Text + 1
If Label4.Text = 1 Then
md5check()
End If
End Sub
Private Sub md5checkdelay2_Tick(sender As System.Object, e As System.EventArgs) Handles md5checkdelay2.Tick
'The timer that adds 1 second delay before it checks the md5code after the file(s) is changed but for the rename function instead
Label7.Text = Label7.Text + 1
If Label7.Text = 1 Then
md5check2()
End If
End Sub
结束类
编辑结束
我之前提出了一个问题,名为“仅监视文件中的 md5 代码的文件夹监视器崩溃”,有人告诉我关闭文件流并使用另一个代码来显示表单。但它不起作用。我制作了一个名为 TestForm 的表单,它不包含任何内容,只包含表单,因为当我尝试显示包含 PictureBoxes 等的 Form2 时整个程序关闭,并且我编写了代码以在 md5 时显示 TestForm修改后的文件的代码和我在代码中指定的md5代码相同,但是显示的表格只显示冻结,文件流不会关闭,我尝试使用 f.Close() 关闭文件流。但是,如果我编写代码以在修改文件的 md5 代码等于我在代码中指定的 md5 代码时显示一个 MsgBox,它就可以正常工作。这是代码:
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 = "D41D8CD98F0B24E980998ECF8427E" Then 'D41D8CD98F0B24E980998ECF8427E is the md5code of a blank txt file
' Dim frm2 As New Form2
' frm2.Show()
TestForm.Show()
f.Close()
Else
f.Close()
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