Listing all sub directories in VB.net

我怕爱的太早我们不能终老 提交于 2021-02-10 19:57:24

问题


can anyone tell me how to list all subfolders in vb.net. i want to put it on a listbox, i have created a code but it only search on the current location, and does not include subfolder. here is my code,,

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

回答1:


Use Directory.GetDirectories() static method (MSDN Reference),

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

Or instance method,

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



回答2:


In VB.NET there is the My namespace, which exposes a variety of infrastructures.

You can get a folder like the following:

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



回答3:


I have added the option to either list just the current folder level, or to do recursion, i.e. list all levels.

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



回答4:


'This code will work according to your question

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


来源:https://stackoverflow.com/questions/8904402/listing-all-sub-directories-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!