Catch the exception inside the for loop, not outside.
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
See if this helps.
1 ListBox, 1 Button
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
getMyCoolFiles("C:\", ListBox1)
'// getMyCoolFiles(path of the folder you want to search, the ListBox to use for adding your found files to)
End Sub
Private Sub getMyCoolFiles(ByVal selectedDirectoryToSearch As String, ByVal ListBoxForFoundFiles As ListBox)
ListBoxForFoundFiles.Items.Clear()
Dim myCoolFolder As New IO.DirectoryInfo(selectedDirectoryToSearch)
For Each foundDirectory In myCoolFolder.GetDirectories '// loop thru all top directories.
Try
'// search top directory and subfolders.
For Each myFoundFile As IO.FileInfo In foundDirectory.GetFiles("*.*", IO.SearchOption.AllDirectories)
ListBoxForFoundFiles.Items.Add(myFoundFile.FullName) '// add File to ListBox.
Next
Catch ex As UnauthorizedAccessException
'MsgBox(ex.Message) '// display Folders that have been Denied accessed to.
End Try
Next
MsgBox("Total Files: " & CInt(ListBoxForFoundFiles.Items.Count).ToString("#,###,###")) '// display Total File Count.
End Sub
End Class
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
chris007, next time start your own thread and add the link for this thread in it, if referring to it.
Otherwise, see if this helps to get all files in a folder.
Dim myCoolFolder As String = "C:\!vbTemp\" '// your folder.
For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
(myCoolFolder, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
ListBox1.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add file name with extension.
Next
'// to search subfolders, change "SearchTopLevelOnly" to "SearchAllSubDirectories".
'// to only get a certain file type, change "*.*" to your file type.
'//-- example for .txt files: "*.txt"
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384