title: FileSystem.GetFiles() + UnauthorizedAccessException error?
body: <p>It <a href="http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/51864316-8afc-4edd-b14d-d20c737b4138" rel="nofollow noreferrer">seems like</a> FileSystem.GetFiles() is unable to recover from the UnauthorizedAccessException exception that .Net triggers when trying to access an off-limit directory.</p>
  
  <p>In this case, does it mean this class/method isn't useful when scanning a whole drive and I should use some other solution (in which case: Which one?)?</p>
  
  <p>Here's some code to show the issue:</p>
  
  <pre><code>Private Sub bgrLongProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgrLongProcess.DoWork
      Dim drive As DriveInfo
      Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
      Dim filepath As String
  
      'Scan all fixed-drives for MyFiles.*
      For Each drive In DriveInfo.GetDrives()
          If drive.DriveType = DriveType.Fixed Then
              Try
                  'How to handle "Access to the path 'C:\System Volume Information' is denied." error?
                  filelist = My.Computer.FileSystem.GetFiles(drive.ToString, FileIO.SearchOption.SearchAllSubDirectories, "MyFiles.*")
                  For Each filepath In filelist
                    DataGridView1.Rows.Add(filepath.ToString, "temp")
                    'Trigger ProgressChanged() event
                    bgrLongProcess.ReportProgress(0, filepath)
                  Next filepath
              Catch Ex As UnauthorizedAccessException
                  'How to ignore this directory and move on?
              End Try
          End If
      Next drive
  End Sub
  </code></pre>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p>Edit: What about using a Try/Catch just to have GetFiles() fill the array, ignore the exception and just resume?</p>
  
  <pre><code>Private Sub bgrLongProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgrLongProcess.DoWork
      'Do lengthy stuff here
      Dim filelist As Collections.ObjectModel.ReadOnlyCollection(Of String)
      Dim filepath As String
  
      filelist = Nothing
      Try
          filelist = My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories, "MyFiles.*")
      Catch ex As UnauthorizedAccessException
          'How to just ignore this off-limit directory and resume searching?
      End Try
  
      'Object reference not set to an instance of an object
      For Each filepath In filelist
          bgrLongProcess.ReportProgress(0, filepath)
      Next filepath
  End Sub
  </code></pre>