Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How Do I search multiple *.exe files using there MD5 hash in a particular recursive directory.


Currently I am using this Code to get *.exe files,But it takes a lot of time when there are many sub directories.


Try
For Each f In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.ProgramFiles, "*.exe", SearchOption.AllDirectories)
ListBox2.Items.Add(f)
Next

Catch ex As UnauthorizedAccessException

End Try

-
And matching each *.exe file MD5 hash with list of MD5 hash in a textbox,


Timer.Tick


Try

ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1

Catch ex As Exception

End Try

Try
Dim sbox As New TextBox
Dim read As String = My.Computer.FileSystem.ReadAllText(MD5-Hash-Box.Text)
sbox.Text = read.ToString
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(ListBox2.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
f = New FileStream(ListBox2.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
md5.ComputeHash(f)
Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Next

If sbox.Text.Contains(buff.ToString) Then
listbox1.items.add(listbox2.selecteditem)
End If

Catch ex As Exception

End Try
Posted
Updated 11-May-14 2:51am
v3

1 solution

There is no such thing as miracle. If you have some value of MD5, you will need to read each file at least once, all its content, calculate MD5 of the data and compare with your MD5 value. It will take time.

There is nothing you can bypass. If you have some silliness in your code, you have to eliminate it, of course, but… Oh, wait a minute, you have one: you are wasting time on considering all the files, not just "*.exe" and calculating EndsWith. You should use file mask instead: http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.110%29.aspx[^].

However, you need to know one Microsoft problem related to it, described here: Directory.Get.Files search pattern problem[^].

You will save some time on it, but I think this optimization would be minor compared to the time needed to read file and calculate MD5.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900