title: Display all drives, files, and subfolders in treeview
body: <p>I'm looking for the simplest way to display all drives, files, and subfolders in a treeview.  If someone's got a snippet of code to do this that they don't mind sharing I would really appreciate it.</p>
  
  <p>The closest I've gotten was this code I tried using, but it gave me a "IOException was unhandled" error saying "The device is not ready." error at runtime (after about 5-10 sec) on the line below</p>
  
  <pre><code>Dim folders() As String = IO.Directory.GetDirectories(dir)
  </code></pre>
  
  <p>underneath is the rest of the code</p>
  
  <pre><code>Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Dim drives As System.Collections.ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = My.Computer.FileSystem.Drives
      Dim rootDir As String = String.Empty
      For i As Integer = 0 To drives.Count - 1
          rootDir = drives(i).Name
          TreeView1.Nodes.Add(rootDir)
          PopulateTreeView(rootDir, TreeView1.Nodes(i))
      Next
  End Sub
  
  Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
      Dim folder As String = String.Empty
      Try
          Dim folders() As String = IO.Directory.GetDirectories(dir)
          If folders.Length &lt;&gt; 0 Then
              Dim childNode As TreeNode = Nothing
              For Each folder In folders
                  childNode = New TreeNode(folder)
                  parentNode.Nodes.Add(childNode)
                  PopulateTreeView(folder, childNode)
              Next
          End If
      Catch ex As UnauthorizedAccessException
          parentNode.Nodes.Add(folder &amp; ": Access Denied")
      End Try
  End Sub
  </code></pre>