【问题标题】:How to compare files in listview using vb.net如何使用 vb.net 比较列表视图中的文件
【发布时间】:2018-05-27 11:00:29
【问题描述】:

我正在从目录中获取文件并将其添加到列表视图中以及它们的哈希值。现在我想做的是从该列表视图中比较相似类型的文件并仅显示存在的重复文件。

我已经这样做了

Public Function PrintByteArray(ByRef array() As Byte)
    Dim hex_value As String = ""
    Dim i As Integer
    For i = 0 To array.Length - 1
        hex_value += array(i).ToString("x2")
    Next i

    Return hex_value.ToLower
End Function

Dim SourceDir As DirectoryInfo = New DirectoryInfo(TextBox1.Text)
    For Each childFile As FileInfo In SourceDir.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.Extension.ToLower = ComboBox1.SelectedItem)

        Dim hash
        hash = MD5.Create
        Dim hashvalue() As Byte
        Dim filestream = childFile.OpenRead
        hashvalue = hash.ComputeHash(filestream)
        Dim has_hex1 = PrintByteArray(hashvalue)

        'Adding in ListView
        Dim NewItem As ListViewItem = ListView1.Items.Add(Path.GetFileNameWithoutExtension(childFile.Name))
        Dim filesize As Long = childFile.Length / 1024
        Dim fileLocation = childFile.FullName
        NewItem.SubItems.Add(filesize)
        NewItem.SubItems.Add(childFile.Extension)
        NewItem.SubItems.Add(has_hex1)
        NewItem.SubItems.Add(fileLocation)

    Next

然而,上面的代码只添加了从目录中检索到的文件。我需要显示目录中相同的文件。请帮帮我。

【问题讨论】:

  • 你所做的工作有用吗?如果不是,具体问题是什么?它究竟在哪里表现出与您的期望相反的行为?这些具体期望是什么?实际行为是什么?
  • 其实上面的代码很简单,它从目录中检索文件及其哈希值并将其显示在列表视图中。相反,我想获取具有相同名称和相同哈希值的重复文件并将其显示在列表视图中。我不知道如何继续它,你能帮我做同样的事情吗?
  • 代码不会在 Option Strict 开启的情况下编译。我们没有 PrintByteArray 函数。
  • @Mary 请查看编辑后的代码
  • “我想获取具有相同名称和相同哈希值的重复文件并在列表视图中显示” - 基本方法:创建自定义数据结构来保存必要的信息,然后为每个文件实例化它的一个实例,并将每个新实例添加到List(Of T) 中。但是,在将项目添加到列表之前迭代整个事物并查看它是否已经包含具有相同数据的条目。如果是,请将列表条目和新项目添加到 ListView

标签: vb.net listview md5 vb.net-2010


【解决方案1】:

我使用目录对话框而不是文本框来获取目录。

Public Class CheckForDuplicateFiles
    Private Sub btnDirectory_Click(sender As Object, e As EventArgs) Handles btnDirectory.Click
        Directory.ShowDialog()
        Dim SelectedDirectory As String = Directory.SelectedPath
        Dim SourceDirectory As DirectoryInfo = New DirectoryInfo(SelectedDirectory)
        Dim lstFiles As New List(Of FileData)
        For Each childFile As FileInfo In SourceDirectory.GetFiles("*", SearchOption.AllDirectories).Where(Function(file) file.Extension.ToLower = cboExtensions.SelectedItem.ToString)
            Dim hash As MD5
            hash = MD5.Create
            Dim hashvalue() As Byte
            Dim filestream = childFile.OpenRead
            hashvalue = hash.ComputeHash(filestream)
            Dim has_hex1 As String = PrintByteArray(hashvalue)
            Dim f As New FileData()
            f.Name = Path.GetFileNameWithoutExtension(childFile.Name)
            f.Size = childFile.Length
            f.Extension = childFile.Extension
            f.Hash = has_hex1
            f.Location = childFile.FullName
            f.AddedToListView = False
            For i As Integer = 0 To lstFiles.Count - 1
                If lstFiles.Item(i).Hash = f.Hash Then
                    AddToListView(f)
                    f.AddedToListView = True
                    If lstFiles.Item(i).AddedToListView = False Then
                        AddToListView(lstFiles.Item(i))
                        lstFiles.Item(i).AddedToListView = True
                    End If
                    Exit For
                End If
            Next
            lstFiles.Add(f)
        Next
    End Sub
    Private Sub AddToListView(file As FileData)
        'Adding in ListView
        Dim NewItem As ListViewItem = ListView1.Items.Add(file.Name)
        NewItem.SubItems.Add(file.Size.ToString)
        NewItem.SubItems.Add(file.Extension)
        NewItem.SubItems.Add(file.Hash)
        NewItem.SubItems.Add(file.Location)
    End Sub
    Private Function PrintByteArray(bytes As Byte()) As String
        Dim sb As New StringBuilder
        For Each b As Byte In bytes
            sb.Append(b)
        Next
        Return sb.ToString
    End Function
    Public Class FileData
        Public Property Name As String
        Public Property Size As Long
        Public Property Extension As String
        Public Property Hash As String
        Public Property Location As String
        Public Property AddedToListView As Boolean
    End Class
End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多