Cargar la estructura de arbol del disco en un TreeView

2386 vistas

Veamos cómo cargar el sistema de carpetas del disco C:\ en un TreeView (estilo al Explorador de Windows).



vbnet
  1. Imports System
  2. Imports System.IO
  3. Imports System.Windows.Forms
  4.  
  5.   '...
  6.  
  7.   Private Sub Explorer(ByVal node As TreeNode)
  8.     Try
  9.       Dim s As String
  10.       node.Nodes.Clear()
  11.         For Each s In Directory.GetDirectories(node.FullPath)
  12.           node.Nodes.Add(Path.GetFileName(s))
  13.         Next
  14.     Catch
  15.     End Try
  16.   End Sub
  17.  
  18.   Private Sub BTNClick(ByVal sender As Object, ByVal e As EventArgs)
  19.     TV.Nodes.Add("C:\")
  20.     Explorer(TV.Nodes.Item(0))
  21.   End Sub
  22.  
  23.   Private Sub TVAfterExpand(ByVal sender As Object, ByVal e As TreeViewEventArgs)
  24.     Dim tn As TreeNode
  25.     For Each tn In e.Node.Nodes
  26.         Explorer(tn)
  27.     Next
  28.   End Sub