0

我无法将此代码转换为允许我将文件拖放到我的应用程序中,并且我的应用程序会创建一个消息框,显示它的 md5 哈希码。目前,我有代码可以提供我放入的文件的目录。

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.AllowDrop = True
End Sub

Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
    For Each path In files
        MsgBox(path)
    Next
End Sub

Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub
End Class
4

1 回答 1

0

好像你有你需要的大部分代码,只需要一个函数来计算 md5,然后你可以将它插入到你的消息框中:

MsgBox(ComputeMD5Hash(path))

这是该功能,主要来自此Microsoft 支持文章

Private Function ComputeMD5Hash(ByVal path As String) As String
    Dim tmpSource() As Byte
    Dim tmpHash() As Byte
    'Create a byte array from source data.
    tmpSource = My.Computer.FileSystem.ReadAllBytes(path)
    'Compute hash based on source data.
    tmpHash = New Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(tmpSource)
    Dim i As Integer
    Dim sOutput As New System.Text.StringBuilder(tmpHash.Length)
    For i = 0 To tmpHash.Length - 1
        sOutput.Append(tmpHash(i).ToString("X2"))
    Next
    Return sOutput.ToString()
End Function

如果您想删除一个目录并获取其中的所有文件的哈希值,您首先需要检查您是在处理文件还是文件夹,如果它是一个文件夹,您循环遍历里面的所有文件并调用函数对于每一个,这将使您的DragDrop事件处理程序看起来像这样,并具有可以处理文件和文件夹的额外好处。

Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
    For Each path In files
        If IO.File.GetAttributes(path) = FileAttribute.Directory Then
            'Dealing with a Directory
            Dim di As New IO.DirectoryInfo(path)
            Dim fiArr As IO.FileInfo() = di.GetFiles()
            Dim fri As IO.FileInfo
            For Each fri In fiArr
                MessageBox.Show(fri.FullName & " " & ComputeMD5Hash(fri.FullName))
            Next fri
        Else
            'Dealing with a File
            MessageBox.Show(path & " " & ComputeMD5Hash(path))
        End If
    Next
End Sub
于 2013-08-09T04:16:57.750 回答