如何使用vb.net获取指定路径中的所有目录和子目录的列表

时间:2011-08-29 07:37:09

标签: vb.net

我的目录结构如下所示。

父目录
---子目录1 ---子目录2 ------子目录2a ------子目录2b ---子目录3

我在VB.net上写文件来获取所有目录名称(包括所有级别的子目录)

但是在使用directory.getfilesystementries(path)时,我只获得了顶级子目录。有关如何获取所有子级别的所有子目录名称的任何想法吗?

4 个答案:

答案 0 :(得分:1)

只需使用以下内容:

Dim result = System.IO.Directory.EnumerateDirectories(path, "*", System.IO.SearchOption.AllDirectories)

诀窍是SearchOption.AllDirectories

BTW:您可以对GetFileSystemEntries-Method

执行相同的操作

答案 1 :(得分:1)

Directoryinfo对象可以提供各种信息和目录,包括目录/文件甚至系统文件

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim dir As New DirectoryInfo("C:\")
    For Each dirItem As DirectoryInfo In dir.GetDirectories
        MsgBox(dirItem.Name)
    Next
End Sub

答案 2 :(得分:1)

Dim di As New DirectoryInfo(FolderName)
di = New DirectoryInfo(path)

rgFiles = di.GetFiles("*.*", IO.SearchOption.AllDirectories)

For Each fi As FileInfo In rgFiles
    If CheckIfExist(fi.FullName.ToString.Replace("\" & fi.Name, "")) = False Then
        ListBox1.Items.Add(fi.FullName.ToString.Replace("\" & fi.Name, ""))
    End If
Next

Public Function CheckIfExist(ByRef Path As String) As Boolean
    Dim RetVal As Boolean = False

    For Each LI As String In ListBox1.Items
        If LI.ToString = Path Then
            RetVal = True
            Return RetVal
            Exit Function
        End If
    Next
    Return RetVal
End Function

答案 3 :(得分:0)

您需要使用指定是否搜索子目录的Directory.GetFileSystemEntries的重载版本:

Dim allDirectories As String() = Directory.GetFileSystemEntries("path", "*", SearchOption.AllDirectories)

Directory.GetFileSystemEntries Method (String, String, SearchOption)