How to list all files in a folder including subfolder files.

I hope it helps :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' make a reference to a directory
    Dim di As New IO.DirectoryInfo("c:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

   'list the names of all files in the specified directory
    For Each dra In diar1
        ListBox1.Items.Add(dra)
    Next
End Sub

Since you have not mentioned which control to add the Files to, I will also use a ListBox.

Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                            (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                'ListBox1.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add only File Name with extension.
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))

To only search the Top Folder and not the SubFolders as well, change FileIO.SearchOption.SearchAllSubDirectories to: FileIO.SearchOption.SearchTopLevelOnly

Since you have not mentioned which control to add the Files to, I will also use a ListBox.

Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                            (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                'ListBox1.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add only File Name with extension.
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###")

To only search the Top Folder and not the SubFolders as well, change FileIO.SearchOption.SearchAllSubDirectories to: FileIO.SearchOption.SearchTopLevelOnly

But it doesn't work when the Directory has many Subdirectory.System/Program gets hanged. Is ther any soulution for this.

See if this helps.
1 Button, 1 ListBox, 1 BackgroundWorker

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync() '// Start BackgroundWorker.
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ListBox1.Items.Clear() '// clear ListBox.
        Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                             (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))
    End Sub
End Class
commented: Helpful! +12

See if this helps.
1 Button, 1 ListBox, 1 BackgroundWorker

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync() '// Start BackgroundWorker.
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ListBox1.Items.Clear() '// clear ListBox.
        Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                             (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))
    End Sub
End Class

Thanks for ur help. But ther is one error in ListBox1.
Error:
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on

How can i solve this error

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

Thank u very much. It works fine . I had forgot to copy that line.

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

One more Help Sir..
how can i get all folders including subfolders.but i do not want list files.

One more Help Sir..
how can i get all folders including subfolders.but i do not want list files.

I got it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.