title: Binary file scanner
body: <p>I'm doing a hexadecimal scanner for files, and I have a database with hex strings of viruses.</p>
  
  <p>I have the database as XML and the scanner in VB.NET.</p>
  
  <p>Goal: perform a simple antivirus (no scanner MD5).</p>
  
  <p>Well, what I want is to list several files in a listbox and go through it and scan the hex fence scanning every one of them, those that match are passed to listbox2.</p>
  
  <pre><code>Imports System.IO
  Imports System.Text
  
  Public Class HexEngine
  
      Dim ArrayHold() As Byte
      Dim Index As Integer = 0
      Dim Str As New StringBuilder
      Dim tStr As String = ""
      Dim tempStr As String = ""
      Dim IndexEnd As Integer = 0
      Dim InputString As String = ""
      Dim a As Integer
  
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          listv.Items.Clear()
          For abc = 0 To ListBox1.Items.Count - 1
  
  
              Scan(ListBox1.Items(a).ToString)
  
          Next abc
      End Sub
  
      Private Sub Scan(ByVal dir As String)
  
  
          Dim myStreamReader As StreamReader = Nothing
          myStreamReader = File.OpenText(dir)
          InputString = myStreamReader.ReadToEnd()
          ArrayHold = Encoding.Default.GetBytes(InputString)
  
          Do
              IndexEnd = Index + 9
  
              For x As Integer = Index To IndexEnd
  
                  If x &gt; UBound(ArrayHold) Then
                      tempStr = tempStr
                  Else
                      tStr = UCase(Convert.ToString(ArrayHold(x), 16))
  
                      If tStr.Length &lt; 2 Then tStr = "0" &amp; tStr
  
                      Str.Append(tStr)
                      tempStr = tempStr &amp; Chr(ArrayHold(x))
  
                  End If
              Next
  
              Index = Index + 10
          Loop While IndexEnd &lt; UBound(ArrayHold)
          For Each signature As XElement In xml.Root.Elements
              If InStr(1, Str.ToString, signature.&lt;hex&gt;.Value, vbTextCompare) Then
                  listv.Items.Add(signature.&lt;name&gt;.Value)
                  If listv.Items.Count &gt; 0 Then
                      Label1.Text = "Virus"
                  Else
                      Label1.Text = "No Virus"
                  End If
              End If
          Next
  
      End Sub
  
      Dim xml = &lt;?xml version="1.0"?&gt;
                &lt;signatures&gt;
                    &lt;signature&gt;
                        &lt;name&gt;Eicar-Test-Signatur (AntiVir)&lt;/name&gt;
                        &lt;hex&gt;58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441&lt;/hex&gt;
                    &lt;/signature&gt;
                    &lt;signature&gt;
                        &lt;name&gt;Hybris.Gen (AntiVir)&lt;/name&gt;
                        &lt;hex&gt;f649e7cc1e00d37e7f3bc85fff3486ac6de91433aa3a39ef1b114d37b534b8323f6ff67132638a3fe2f2afb4aaf9b7e3b4669bb3cab028298aab533c5d73546cdd396fd58c2c7734c50bca68eb709b889a086fb3db5f8ae533a4d5816e8c5f560983695efa14e291c204b1316e657773&lt;/hex&gt;
                    &lt;/signature&gt;
                &lt;/signatures&gt;
  
      'Dim files As List(Of FileInfo) = FileDirSearcher.GetFiles("C:\Windows\System32", SearchOption.AllDirectories).ToList
      ' Dim dirs As List(Of DirectoryInfo) = FileDirSearcher.GetDirs("C:\Windows\System32", SearchOption.AllDirectories).ToList
  
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          Dim startPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
          Dim filess As IEnumerable(Of FileInfo) = FileDirSearcher.GetFiles(dirPath:=startPath,
                                                                   searchOption:=SearchOption.TopDirectoryOnly,
                                                                   fileNamePatterns:={"*"},
                                                                   fileExtPatterns:={"*.vbs", "*.exe"},
                                                                   ignoreCase:=True,
                                                                    throwOnError:=True)
  
          For Each File In Get_All_Files(startPath, False)
              ListBox1.Items.Add(File)
          Next
  
      End Sub
  End Class
  
  #Region " Get All Files Function "
  
  ' [ Get All Files Function ]
  '
  ' // By Elektro H@cker
  '
  ' Examples:
  '
  ' Dim Files As Array = Get_All_Files("C:\Test", True)
  ' For Each File In Get_All_Files("C:\Test", False) : MsgBox(File) : Next
  
  Private Function Get_All_Files(ByVal Directory As String, Optional ByVal Recursive As Boolean = False) As Array
      If System.IO.Directory.Exists(Directory) Then
          If Not Recursive Then : Return System.IO.Directory.GetFiles(Directory, "*", IO.SearchOption.TopDirectoryOnly)
          Else : Return IO.Directory.GetFiles(Directory, "*", IO.SearchOption.AllDirectories)
          End If
      Else
          Return Nothing
      End If
  End Function
  
  #End Region
  </code></pre>
  
  <p>Well, as you can see, I already did everything, but still it is very slow and it does not scan the files of the listbox.</p>
  
  <p>I just want a hex scanner that scans all the files that are in a listbox and that the scanner is fast.</p>