我对VB.Net(2008,Express Edition)还不够了解,所以我想问是否有更好的方法来查找具有不同名称但内容相同的文件,即。重复。
在下面的代码中,我使用 GetFiles() 检索给定目录中的所有文件,对于每个文件,使用 MD5 哈希其内容,检查该值是否已存在于字典中:如果是,则它是重复的,我将删除它;如果没有,我会将此文件名/哈希值添加到字典中以供稍后使用:
'Get all files from directory
Dim currfile As String
For Each currfile In Directory.GetFiles("C:\MyFiles\", "File.*")
'Check if hashing already found as value, ie. duplicate
If StoreItem.ContainsValue(ReadFileMD5(currfile)) Then
'Delete duplicate
'This hashing not yet found in dictionary -> add it
Else
StoreItem.Add(currfile, ReadFileMD5(currfile))
End If
Next
Run Code Online (Sandbox Code Playgroud)
这是解决查找重复项问题的好方法,还是有我应该知道的更好方法?
谢谢。
您可以通过以下方式进行优化
我确信还有很多其他人。