【发布时间】: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