title: Searching a drive with vb.net
body: <p>I am making a program that searches your drive for an executable.</p>
  
  <pre><code>Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
      Dim di As New DirectoryInfo("C:\")
      Dim files() As FileInfo
      Dim a As Integer
      Do While a = 0
          Try
              files = di.GetFiles("FileName.exe", SearchOption.AllDirectories)
          Catch ex As UnauthorizedAccessException
          End Try
          If Not files Is Nothing Then
              a = 1
          End If
      Loop
      txt_Location.Text = files(0).FullName
  End Sub
  </code></pre>
  
  <p>As soon as it hits the first UnauthorizedAccessException, it gets stuck in an infinite loop. How do I skip over the files that produce the exception?</p>
  
  <p>EDIT:
  This is what I have now:</p>
  
  <pre><code>Public Sub DirSearch(ByVal sDir As String)
      Dim dir As String
      Try
          For Each dir In Directory.GetDirectories(sDir)
              For Each file In Directory.GetFiles(dir, "Filename.exe")
                  ComboBox1.Items.Add(file)
              Next
              DirSearch(dir)
          Next
      Catch ex As Exception
          Debug.WriteLine(ex.Message)
      End Try
  End Sub
  
  Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
  DirSearch("C:\")
  End Sub
  </code></pre>