开发者

Get all folder / directories list in VB.net

开发者 https://www.devze.com 2022-12-22 12:34 出处:网络
This is my first Stackoverflow question, I\'m learning VB and having a few problems with getting a list of all folders/directories on the system. I\'m using the code included here and it seems to work

This is my first Stackoverflow question, I'm learning VB and having a few problems with getting a list of all folders/directories on the system. I'm 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开发者_如何学Python to listbox or text etc here
    Next

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? I'd like to know what is causing this first, and a good fix, or alternative way to do this.

Thanks in advance.


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.


Try
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirectoryList.AddRange(Dirs)
    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
    Next
    Catch ex As Exception
End Try


You have a double End Sub in your code!

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号