I'm writing a program that gathers a list of files within a directory and optionally, every file within the subdirectories (10 levels deep) of the directory then exports it as a text file. I have no idea how to do this so I just attempted to code it myself.

It works by gathering a list of every subdirectory within the selected directory and their subdirectories, so on and so on and adds them to a listbox. Then to gather the list of files I used a for each to get the files within the subdirectories but it shows "(Collection)". After exporting it shows "
System.Collections.ObjectModel.ReadOnlyCollection`1[System.String]".

How can I make this code work? I think I could've done this much easier a different way but I need the full path included.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            ListBox1.Items.Clear()
            Dim FolderBrowserDialog1 As New FolderBrowserDialog
            With FolderBrowserDialog1
                .RootFolder = Environment.SpecialFolder.Desktop
                .SelectedPath = "C:\"
                If .ShowDialog = DialogResult.OK Then
                    For Each File In My.Computer.FileSystem.GetFiles(.SelectedPath)
                        ListBox1.Items.Add(File)
                    Next
                    If CheckBox1.Checked = True Then
                        ListBox3.Items.Clear()
                        For Each Directory In My.Computer.FileSystem.GetDirectories(.SelectedPath)
                            ListBox3.Items.Add(Directory)
                            For Each level1 In My.Computer.FileSystem.GetDirectories(Directory)
                                ListBox3.Items.Add(level1)
                                For Each level2 In My.Computer.FileSystem.GetDirectories(level1)
                                    ListBox3.Items.Add(level2)
                                    For Each level3 In My.Computer.FileSystem.GetDirectories(level2)
                                        ListBox3.Items.Add(level3)
                                        For Each level4 In My.Computer.FileSystem.GetDirectories(level3)
                                            ListBox3.Items.Add(level4)
                                            For Each level5 In My.Computer.FileSystem.GetDirectories(level4)
                                                ListBox3.Items.Add(level4)
                                                For Each level6 In My.Computer.FileSystem.GetDirectories(level5)
                                                    ListBox3.Items.Add(level6)
                                                    For Each level7 In My.Computer.FileSystem.GetDirectories(level6)
                                                        ListBox3.Items.Add(level7)
                                                        For Each level8 In My.Computer.FileSystem.GetDirectories(level7)
                                                            ListBox3.Items.Add(level8)
                                                            For Each level9 In My.Computer.FileSystem.GetDirectories(level8)
                                                                ListBox3.Items.Add(level9)
                                                                For Each level10 In My.Computer.FileSystem.GetDirectories(level9)
                                                                    ListBox3.Items.Add(level10)
                                                                Next
                                                            Next
                                                        Next
                                                    Next
                                                Next
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                        For Each Item In ListBox3.Items
                            If Item = String.Empty Then
                                ListBox3.Items.Remove(Item)
                            End If
                            ListBox1.Items.Add(My.Computer.FileSystem.GetFiles(Item))
                        Next
                    End If
                End If
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

I figured it out after lots of trial and error. I had to do a for each for the files and the subdirectories not just the subdirectories.

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.