Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的文件系统监视程序代码VB.NET有什么问题?_Vb.net_Antivirus - Fatal编程技术网

我的文件系统监视程序代码VB.NET有什么问题?

我的文件系统监视程序代码VB.NET有什么问题?,vb.net,antivirus,Vb.net,Antivirus,当我的文件系统监视程序检测到病毒时,会显示一个对话框,但当我单击“删除文件”选项时,它会在我的程序中显示它已打开,但当我添加“openfiledialog”时,它会在文件系统监视程序中显示。Dispose函数它不会显示我的对话框,因此代码在这里有人可以提供修复吗?代码如下 Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) H

当我的文件系统监视程序检测到病毒时,会显示一个对话框,但当我单击“删除文件”选项时,它会在我的程序中显示它已打开,但当我添加“openfiledialog”时,它会在文件系统监视程序中显示。Dispose函数它不会显示我的对话框,因此代码在这里有人可以提供修复吗?代码如下

Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
    Try
        Detect.Labellastreal.Text = e.FullPath
        ListBox3.Items.Add(Detect.Labellastreal.Text)
        Me.OpenFileDialog3.FileName = ""
        Dim scanbox As New TextBox
        scanbox.Text = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\VirusList.dat").ToString
        Dim md5 As New MD5CryptoServiceProvider
        Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
        f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
        md5.ComputeHash(f)
        Dim hash As Byte() = md5.Hash
        Dim buff As New StringBuilder
        Dim hashByte As Byte
        For Each hashByte In hash
            buff.Append(String.Format("{0:X2}", hashByte))
        Next
        f.Close()
        If scanbox.Text.Contains(buff.ToString) Then
            Me.OpenFileDialog3.FileName = e.FullPath
            Detect.ShowDialog()


            WriteToLog("Virus detected")

        End If

    Catch exception1 As Exception
        ProjectData.SetProjectError(exception1)
        Dim ex As Exception = exception1
        ProjectData.ClearProjectError()
    End Try
End Sub

首先,甚至不用麻烦使用该代码
FileSystemWatcher
不可靠,经常失败

其次,删除
f=newfilestream(例如,FullPath,FileMode.Open,FileAccess.Read,FileShare.Read,&H2000)
,就像您两次调用它一样

最后,对于具有实时保护的Visual Basic防病毒软件,我建议您使用以下代码:

Private Function GetMD5String(ByVal strFilename As String) As String
        Dim MD5 As String = GetMD5String(strFilename)
        Dim cMD5 = Security.Cryptography.MD5.Create
        Dim bytHash As Byte()
        Dim sb As New System.Text.StringBuilder
        Dim scanbox As New TextBox
        scanbox.Text = My.Computer.FileSystem.ReadAllText("viruslist.txt").ToString
        Me.OpenFileDialog1.FileName = strFilename

        Using cStream As New IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)

            bytHash = cMD5.ComputeHash(cStream)
        End Using

        For Each c In bytHash
            sb.Append(c.ToString("X2"))
        Next

        If scanbox.Text.Contains(sb.ToString) Then
            Detect.ShowDialog()
            WriteToLog("Malicious exploit detected.")
            Quarantinevb.ListBox1.Items.Add(strFilename)
        End If

        Return sb.ToString



    End Function

工作良好且可靠。

我第一次发帖时,所有的布局都错了。。。从Private Sub开始是代码的开始,很抱歉,您正在创建两个
文件流,立即放弃第一个文件流。那么我该怎么做来修复我的代码呢?谢谢您的回复,请不要在需要文件流时打开两个文件流。