Yeah, the exception is
UnauthorizedAccessException
, which is why I recommended that you should test it. There are also 2 places where the exception can happen, so you need to trap them both separately. Try this; it works on my system:
Private Sub ListFiles(ByVal lst As ListBox,
ByVal pattern As String, ByVal dir_info As DirectoryInfo)
' Get the files in this directory.
Dim fs_infos() As FileInfo
Try
fs_infos = dir_info.GetFiles(pattern)
For Each fs_info As FileInfo In fs_infos
lst.Items.Add(fs_info.FullName)
Next fs_info
Catch ex As UnauthorizedAccessException
End Try
fs_infos = Nothing
' Search subdirectories.
Dim subdirs() As DirectoryInfo
subdirs = dir_info.GetDirectories()
Try
For Each subdir As DirectoryInfo In subdirs
ListFiles(lst, pattern, subdir)
Next subdir
Catch ex As UnauthorizedAccessException
End Try
End Sub