編集: コードを変更しようとしました。代わりに別のサブを使用してください。しかし今、何かが変わると、プログラムはクラッシュするだけです。ログを無効にしたり、ブレークポイントを設定したりしましたが、プログラムは長くは続きません。これは、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 コードのみを監視するフォルダー モニターがクラッシュする」という名前の質問をしたことがあります。しかし、うまくいきません。PictureBoxes などを含む Form2 を表示しようとすると、プログラム全体が終了したため、フォームだけを含む 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