vb.net 获取VB.net中的所有文件夹/目录列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2428136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:45:59  来源:igfitidea点击:

Get all folder / directories list in VB.net

vb.net

提问by xpda

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

这是我的第一个 Stackoverflow 问题,我正在学习 VB 并且在获取系统上所有文件夹/目录的列表时遇到了一些问题。我正在使用此处包含的代码,它似乎可以正常工作,直到它到达回收站文件夹和其他一些系统文件夹

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

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.

提前致谢。

回答by xpda

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-Catch 块 Directory.GetDirectories(StartPath),也可以事先检查文件夹的属性。

回答by nope

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

回答by DIIISS

You have a double End Subin your code!

End Sub的代码中有一个 double !

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