2012-01-18 243 views
1

誰能告訴我如何列出vb.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 

回答

1

使用Directory.GetDirectories()靜態方法(MSDN Reference),

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

或實例方法,

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

我應該在搜索模式中放什麼?在「搜索」上 –

+0

使用通配符字符*。*或*或C *等。 – adatapost

+1

在驅動器上使用時總是崩潰。嘗試一下。 –

2

在VB.NET中存在My命名空間,其暴露各種基礎設施。

你可以得到一個文件夾,如下所示:

My.Computer.FileSystem.GetDirectories("your directory", 
    SearchOption.SearchAllSubDirectories, "*") 
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 
+0

解釋爲什麼它會工作會很好 –

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 
相關問題