title: How to populate a treeview to display only a folder and its sub-directories
body: <p>I will be very grateful for your help on this.
  I am trying to populate a treeview to show only a directory as its main root and its sub directories. Other codes and tutorials online show drives and other special folders.</p>
  
  <p>I wan to show only a folder path like:
  C:\Main Folder\Subdirectory1\Subdirectory2 etc. in the nodes</p>
  
  <p>This is all I have and its not helping.</p>
  
  <pre><code>Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
      'Get a list of drives
      Dim drives As DriveInfo() = DriveInfo.GetDrives()
      Dim rootDir As String = String.Empty
      'Now loop thru each drive and populate the treeview
      For i As Integer = 0 To drives.Length - 1
          rootDir = drives(i).Name
          'Add this drive as a root node
          TreeView1.Nodes.Add(rootDir)
          'Populate this root node
          PopulateTreeView(rootDir, TreeView1.Nodes(i))
      Next
  
  End Sub
  Private Sub PopulateTreeView(dir As String, parentNode As TreeNode)
      Dim folder As String = String.Empty
      Try
          Dim folders As String() = System.IO.Directory.GetDirectories(dir)
          If folders.Length &lt;&gt; 0 Then
              Dim childNode As TreeNode = Nothing
              For Each folder_loopVariable As String In folders
                  folder = folder_loopVariable
                  childNode = New TreeNode(folder)
                  childNode.Nodes.Add("")
                  parentNode.Nodes.Add(childNode)
              Next
          End If
          Dim files As String() = System.IO.Directory.GetFiles(dir)
          If files.Length &lt;&gt; 0 Then
              Dim childNode As TreeNode = Nothing
              For Each file As String In files
                  childNode = New TreeNode(file)
                  parentNode.Nodes.Add(childNode)
              Next
          End If
      Catch ex As UnauthorizedAccessException
          parentNode.Nodes.Add(folder &amp; Convert.ToString(": Access Denied"))
      End Try
  End Sub
  </code></pre>