tags:

views:

35

answers:

1

Hi, this is my first Stackoverflow question, im learning VB and having a few problems with getting a list of all folders/directories on the system, im using the code included here and it seems to work until it hits the recycle bin folder, and some other system folders

Sub main()

    Dim DirList As New ArrayList
    GetDirectories("c:\", DirList)


    For Each item In DirList
        'add item to listbox or text etc here
    Next

End sub

End Sub

Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirectoryList.AddRange(Dirs)

    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
    Next
End Sub

Can anyone help me with this, id like to know what is causing this first, and a good fix, or alternative way to do this.

Thanks in advance.

+1  A: 

Access to some folders is not allowed. You can use a Try-Catch block around the Directory.GetDirectories(StartPath), or you can check the properties of the folder beforehand.

xpda
Thanks xpda, I had tried a "on error resume next without much luck, but a "Try Catch" works perfect for my needs, this takes about 2 seconds on my SSD to find 50k of folders and around 12 Seconds on my laptops 5400RPM drive to find 32k of folders.... fast enough for my needs anyway Thank you thumbs up! If anyone has a better suggestion of how to fetch the complete directory structure either faster or in a more .net friendly way please post.
Henry