列出VB.net中的所有子目录

时间:2012-01-18 02:25:28

标签: vb.net directory filesystems .net

任何人都可以告诉我如何在vb.net中列出所有子文件夹。 我想把它放在一个列表框上,我创建了一个代码,但它只搜索当前位置,并且不包含子文件夹。这是我的代码,

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dnum, fnum As Integer


        For Each drive As String In Directory.GetLogicalDrives()
            ListBox1.Items.Add(drive)
        Next drive

        Do While dnum < ListBox1.Items.Count - 3


            Dim di As New DirectoryInfo(ListBox1.Items(dnum))
            'for every subdirectory in the folder, add its name to the listbox
            For Each subdi As DirectoryInfo In di.GetDirectories
                ListBox2.Items.Add(subdi.Name)
            Next
            dnum = dnum + 1

        Loop

        dnum = 0
        Do While dnum < ListBox1.Items.Count - 2
            fnum = 0
            Do While fnum < ListBox2.Items.Count
                Dim loc As String


                loc = (ListBox1.Items(dnum) + ListBox2.Items(fnum))

                Try
                    Dim di As New DirectoryInfo(loc)
                    'for every subdirectory in the folder, add its name to the listbox
                    For Each subdi As DirectoryInfo In di.GetDirectories
                        ListBox3.Items.Add(subdi.Name)
                    Next
                Catch ex As Exception
                End Try

                fnum = fnum + 1

            Loop
            dnum = dnum + 1
        Loop
    End Sub
End Class

4 个答案:

答案 0 :(得分:2)

在VB.NET中有My命名空间,它公开了各种基础结构。

您可以获得如下文件夹:

My.Computer.FileSystem.GetDirectories("your directory", 
  SearchOption.SearchAllSubDirectories, "*")

答案 1 :(得分:1)

使用Directory.GetDirectories()静态方法(MSDN Reference),

System.IO.Directory.GetDirectories("path","searchpattern",SearchOption.AllDirectories)

或实例方法,

Dim di As New DirectoryInfo(Loc)
di.GetDirectories("search", SearchOption.AllDirectories)

答案 2 :(得分:1)

我添加了选项,既可以列出当前文件夹级别,也可以进行递归,即列出所有级别。

Private Sub ListDirectories(RootFolder As String)
    For Each drvs In Directory.GetDirectories(RootFolder)
        '--\\ Display the folder
        lstFolders.Items.Add(drvs)              
        '--\\ Checkbox determines whether or not to display only the current folders
        If chkTopLevelOnly.Checked = False Then 
            If drvs.ToString.Length > 0 Then
                Try
                    For Each di In Directory.GetDirectories(drvs)
                        lstFolders.Items.Add(di & "\")
                        Application.DoEvents()
                        '--\\ Do recursive call to this routine until 
                        '--\\ the last branch has been reached
                        ListDirectories(di)
                        Application.DoEvents()
                    Next
                Catch ex As Exception

                End Try
            End If
        End If
    Next
End Sub

答案 3 :(得分:0)

'此代码将根据您的问题运作

Imports System.IO
Imports System.Management
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each drv In Environment.GetLogicalDrives()
            ComboBox1.Items.Add(drv)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each drvs In Directory.GetDirectories(ComboBox1.Text)
            ListBox1.Items.Add(drvs)
            If drvs.ToString.Length > 0 Then
                Try
                For Each di In Directory.GetDirectories(drvs)
                    ListBox1.Items.Add(di)
                    Next
                Catch ex As Exception

                End Try
            End If

        Next
    End Sub
End Class