# Defaulting Treeview to display all drives



## sdhayes (Apr 14, 2012)

I've googled and been unable to find a working solution to the following.

Is there a way to default a treeview on opening of a form to show all drives? I know it isn't possible to default it to My Computer, which is what I'd really want.

I am using the following code from Load a folder tree in a TreeView efficiently - VB.NET - Source Code | DreamInCode.net

Friend WithEvents MyTreeView As System.Windows.Forms.TreeView
Public Sub LoadFolderTree(ByVal path As String)
Dim basenode As System.Windows.Forms.TreeNode
If IO.Directory.Exists(path) Then
If path.Length <= 3 Then
basenode = MyTreeView.Nodes.Add(path)
Else
basenode = MyTreeView.Nodes.Add(My.Computer.FileSystem.GetName(path))
End If
basenode.Tag = path
LoadDir(path, basenode)
Else
Throw New System.IO.DirectoryNotFoundException()
End If
End Sub

Private Sub MyTreeView_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles MyTreeView.AfterExpand
Dim n As System.Windows.Forms.TreeNode
For Each n In e.Node.Nodes
LoadDir(n.Tag, n)
Next
End Sub

Public Sub LoadDir(ByVal DirPath As String, ByVal Node As Windows.Forms.TreeNode)
On Error Resume Next
Dim Dir As String
Dim Index As Integer
If Node.Nodes.Count = 0 Then
For Each Dir In My.Computer.FileSystem.GetDirectories(DirPath)
Index = Dir.LastIndexOf("\")
Node.Nodes.Add(Dir.Substring(Index + 1, Dir.Length - Index - 1))
Node.LastNode.Tag = Dir
Node.LastNode.ImageIndex = 0
Next
End If
End Sub

Then in my Form Load I am passing path as "C:\" currently. This works, although I am yet to add the checkbox function. On the target machines, the required folders could be on C, D, E or a combination of each.

Is there a way I can list all drives? I'm not brilliant at recursion, so any examples if it is possible would be great. If it isn't possible, then I'll just runtime the drive selection into multiple treeviews, which is unelegant.

Thanks for any help

Stu.


----------



## AceInfinity (Jan 21, 2012)

sdhayes said:


> I've googled and been unable to find a working solution to the following.
> 
> Is there a way to default a treeview on opening of a form to show all drives? I know it isn't possible to default it to My Computer, which is what I'd really want.


Firstly, are you sure? It sounds like you're still just starting out in VB so to make such quick accusations is a bit 'arrogant' for lack of a better term here.



sdhayes said:


> I am using the following code from Load a folder tree in a TreeView efficiently - VB.NET - Source Code | DreamInCode.net
> 
> Friend WithEvents MyTreeView As System.Windows.Forms.TreeView
> Public Sub LoadFolderTree(ByVal path As String)
> ...


Try this:

```
Dim ParentNode As New TreeNode("My Computer")
ParentNode.Nodes.AddRange(System.IO.DriveInfo.GetDrives.Select(Function(n) New TreeNode(n.Name)).ToArray)

TreeView1.Nodes.Add(ParentNode)
```
Preview:









This should get you started anyways.


----------



## sdhayes (Apr 14, 2012)

In regards to 'cannot do' comment, perhaps I should have said that after three hours of Googling, and several threads saying specifically that you could not pass My Computer as a normal folder, I'd been unable to find a way :smile:

Thanks for the code help. Although it won't run with the LoadDirTree function (cannot convert treenode to string), I should be able to get the node values and pass these through :smile:


----------



## sdhayes (Apr 14, 2012)

Updated to say resolved by using GetLogicalDrives as an array and passing this to LoadDir for each drive (elmininating the need for the My Computer start, and also eliminating any Node to string conversion issues). 

Sometimes the obvious answer is the one kicking us in the back of the head!


----------



## AceInfinity (Jan 21, 2012)

If you get the node's value, that is on way you could get the string, simple conversions through a simple LINQ lambda expression in my opinion. 

A node itself is not in reference to the actual text you see, it's the actual object; a node. Which contains the properties on how that node is displayed, it's parent node, etc...

If you need further help just ask.


----------

