EDN Admin
Well-known member
Hello Everyone ! Youre doing a fantastic job on forums by helping everyone.
I have a problem with my directory scan program, means my program is not able to search all the files, can anyone help me how to do this, basically I am searching entire directory for some files whose md5 hash matches with one whose list I have in my project
resource. I am using the below code.
<pre class="prettyprint lang-vb Dim FD As New FolderBrowserDialog
If FD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
TextBox1.Text = FD.SelectedPath
frmCustomizedScan.Label10.Text = TextBox1.Text
ListBox1.Items.Add(TextBox1.Text)
DirSearch(TextBox1.Text, 3)
Dim referenceMD5s = IO.File.ReadAllLines(String.Concat(Application.StartupPath, "mscom.dll"))
Try
For Each file In IO.Directory.GetFiles(FD.SelectedPath, "*.*", IO.SearchOption.AllDirectories)
Dim MD5 = CreateMD5StringFromFile(file)
If referenceMD5s.Contains(MD5) Then
ListBox1.Items.Add(file)
frmIntrusionDetections.ListBox2.Items.Add(file)
End If
Next
Catch ex As Exception
End Try
End If
Private Shared Function CreateMD5StringFromFile(ByVal Filename As String) As String
Dim MD5 = System.Security.Cryptography.MD5.Create
Dim Hash As Byte()
Dim sb As New System.Text.StringBuilder
Using st As New IO.FileStream(Filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite, 8192)
Hash = MD5.ComputeHash(st)
End Using
For Each b In Hash
sb.Append(b.ToString("X2"))
Next
Return sb.ToString
End Function
Sub DirSearch(ByVal sDir As String, ByVal Indent As Integer)
Indent += 3
Try
For Each d As String In Directory.GetDirectories(sDir)
ListBox1.Items.Add((Indent) & d)
DirSearch(d, Indent)
Next
Catch x As System.Exception
ListBox1.Items.Add((Indent) & "**" & x.Message & "**")
End Try
For Each f As String In Directory.GetFiles(sDir, "*.*")
Dim FI As FileInfo = New FileInfo(f)
ListBox1.Items.Add((Indent) & FI.Name)
Next
End Sub[/code]
Any help will be appreciated.
Thanks
View the full article
I have a problem with my directory scan program, means my program is not able to search all the files, can anyone help me how to do this, basically I am searching entire directory for some files whose md5 hash matches with one whose list I have in my project
resource. I am using the below code.
<pre class="prettyprint lang-vb Dim FD As New FolderBrowserDialog
If FD.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
TextBox1.Text = FD.SelectedPath
frmCustomizedScan.Label10.Text = TextBox1.Text
ListBox1.Items.Add(TextBox1.Text)
DirSearch(TextBox1.Text, 3)
Dim referenceMD5s = IO.File.ReadAllLines(String.Concat(Application.StartupPath, "mscom.dll"))
Try
For Each file In IO.Directory.GetFiles(FD.SelectedPath, "*.*", IO.SearchOption.AllDirectories)
Dim MD5 = CreateMD5StringFromFile(file)
If referenceMD5s.Contains(MD5) Then
ListBox1.Items.Add(file)
frmIntrusionDetections.ListBox2.Items.Add(file)
End If
Next
Catch ex As Exception
End Try
End If
Private Shared Function CreateMD5StringFromFile(ByVal Filename As String) As String
Dim MD5 = System.Security.Cryptography.MD5.Create
Dim Hash As Byte()
Dim sb As New System.Text.StringBuilder
Using st As New IO.FileStream(Filename, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite, 8192)
Hash = MD5.ComputeHash(st)
End Using
For Each b In Hash
sb.Append(b.ToString("X2"))
Next
Return sb.ToString
End Function
Sub DirSearch(ByVal sDir As String, ByVal Indent As Integer)
Indent += 3
Try
For Each d As String In Directory.GetDirectories(sDir)
ListBox1.Items.Add((Indent) & d)
DirSearch(d, Indent)
Next
Catch x As System.Exception
ListBox1.Items.Add((Indent) & "**" & x.Message & "**")
End Try
For Each f As String In Directory.GetFiles(sDir, "*.*")
Dim FI As FileInfo = New FileInfo(f)
ListBox1.Items.Add((Indent) & FI.Name)
Next
End Sub[/code]
Any help will be appreciated.
Thanks
View the full article