Question How to add network/external drives/shares to treeview

gyclone

Member
Joined
Jun 1, 2009
Messages
9
Programming Experience
1-3
Greetings,

I have an Explorer style form with a treeview and listview. Everything works exactly how I want it to on local drives, but if I am attached to network drives or have an external drive attached I get the following error:

"Specified argument was out of the range of valid values. Parameter name: index."

If I "Continue" past the error, the drives show up in the treeview, but they can't be expanded. The expand/collapse handle isn't there, as though they drive node doesn't have any child nodes.

I've been looking online for days and can't find anyway to solve this problem. I'd greatly appreciate some assistance.

Here is the code for adding nodes to the tree view:

VB.NET:
Public Sub FillTreeView()
        'Counter for our Physical Drives
        Dim x As Integer
        'Start looping through the Drives
        For x = 0 To My.Computer.FileSystem.Drives.Count - 1
            'make sure the drive is ready
            If My.Computer.FileSystem.Drives(x).IsReady = True Then
                'add the Drive to the Tree Node use the Drive Name as the "Key" and "Text"
                tv_Directories.Nodes.Add(My.Computer.FileSystem.Drives(x).Name, My.Computer.FileSystem.Drives(x).Name)
                'set the Tag Property to the Drive Name for Identification Later On
                tv_Directories.Nodes(My.Computer.FileSystem.Drives(x).Name).Tag = My.Computer.FileSystem.Drives(x).Name
                'add the first level of sub directories to the TreeView
                For Each SubDirectory As String In My.Computer.FileSystem.GetDirectories(My.Computer.FileSystem.Drives(x).Name)
                    'The Mid Function is used so our Node does not include something like
                    '"c:\Windows" it should rather read something like "Windows".
                    'However the Key (in our case the first part of the Add() will
                    'have the whole path. This will be used later for Finding the 
                    'Sub Directories)
                    tv_Directories.Nodes(x).Nodes.Add(SubDirectory, Mid(SubDirectory, 4))
                    'Here we add the Whole path to the Tag Property for Identification
                    'later on
                    tv_Directories.Nodes(x).Nodes(SubDirectory).Tag = SubDirectory
                Next
            End If
        Next
        If tv_Directories.Nodes.Count > 0 Then
            tv_Directories.SelectedNode = tv_Directories.Nodes(0)
        End If

    End Sub
 
Back
Top