ListBox2.Items.Clear()
        Dim myCoolFolder As String = "F:\"
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetDirectories _
                                             (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                ListBox2.Items.Add(myCoolFile)
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox2.Items.Count).ToString("#,###,###"))

This code is for listing all folders including subfolders.
but when i run this code i m getting error "Access to Path F:\System Volume Information is Denied"
How can i ignore this folder and continue scan.?
is ther any solution for this error?
Any help would be greatly appreciated.

Catch the exception inside the for loop, not outside.

It does not work... Again same error..

Post how you did it.

1)

ListBox2.Items.Clear()
        Dim myCoolFolder As String = "F:\"

        For Each myCoolFile As String In My.Computer.FileSystem.GetFiles(myCoolFolder,             FileIO.SearchOption.SearchAllSubDirectories, "*.*")
            Try
                ListBox2.Items.Add(myCoolFile)
            Catch ex As UnauthorizedAccessException
                MsgBox(ex.Message)
            End Try
        Next

       
        MsgBox("Total Files: " & CInt(ListBox2.Items.Count).ToString("#,###,###"))

Error: Access to the path 'F:\System Volume Information\' is denied.

2)

ListBox2.Items.Clear()
        Dim myCoolFolder As String = "F:\"
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles(myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")

                ListBox2.Items.Add(myCoolFile)
         Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)

        Next
        End Try

        MsgBox("Total Files: " & CInt(ListBox2.Items.Count).ToString("#,###,###"))

Errors:
1) 'For' must end with Next
2) 'Next' must be preceded by a matching 'For'

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
Comments
Thank U

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

Thank U. it works.

hi guys.. i tried that and it doesnt work 100%... i mean..i put this folder C:\Users\chris\Documents .. if i search for *.docx, it finds only the docx files that are in SUBFOLDERS inside the Documents.. it does NOT find docx files that are in C:\Users\chris\Documents . i tried C:\Users\chris\Documents C:\Users\chris\Documents\ C:\Users\chris\Documents\\ but nothing works(without \ with 1\ and with double\\). then i tried other folders and i see that it doesnt find the files that are in the "root" (for example C:\Users\chris). if i look to C:\Users\chris it will find files that are in SUBFOLDERS inside the chris but not the files that are in chris without subfolders

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"
This question has already been answered. Start a new discussion instead.