Question : Treeview problem - not display folders on all drives

I am using the attached code snippet to populate a treeview.

There are 2 things it is not doing quite right for me. (I've posted another question about one of the issues.)

The code searches for files of a certain file type (e.g. *.ttf) and populates a treeview. It works fine for the C drive but only displays the D and E drives on my computer as an empty node.

Can someone please tell me what I am missing?

Thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
Private Sub GetFiles(ByVal e As String, ByVal dg As DataGridView)
        Dim path As String, i As Int32 = 0
        ToolStripProgressBar1.Step = 1
 
        ' grow the tree
        Me.TreeView1.BeginUpdate()
 
        For i = 0 To theDrives.Length() - 1 ' array of all hard drives on the computer
            Try
                path = theDrives(i, 0).ToString()
                If path <> "Nothing" Then
                    Me.TreeView1.Nodes.Add(path.ToString())
                    AddDirectoriesNFiles(Me.TreeView1.Nodes(0), path.ToString(), "*.ttf")
                End If
            Catch ex As Exception
 
            End Try
        Next
        Me.TreeView1.EndUpdate()
    End Sub
 
Private Sub AddDirectoriesNFiles(ByVal rootNode As TreeNode, ByVal dir As String, ByVal fileMatch As String)
 
        Dim node As TreeNode
        Dim theFile As String = ""
 
        'Loop through each folder in the directory
        For Each di As IO.DirectoryInfo In _
            New IO.DirectoryInfo(dir).GetDirectories()
 
            'Add a new node
            node = New TreeNode(di.Name)
            rootNode.Nodes.Add(node)
 
            'If there are subfolders, then loop through them
            Try
                AddDirectoriesNFiles(node, di.FullName, fileMatch)
             Catch
                MessageBox.Show("Insufficient rights for the folder")
            End Try
        Next
 
        For Each curFile As String In IO.Directory.GetFiles(dir, fileMatch)
            ToolStripStatusLabel4.Text = curFile.ToString()
            theFile = curFile.ToString()
            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)
            ToolStripProgressBar1.PerformStep()
            If ToolStripProgressBar1.Value = 100 Then
                ToolStripProgressBar1.Value = 1
            End If
            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)
            rootNode.Nodes.Add(New TreeNode(curFile))
            FilesCounted = FilesCounted + 1
            ToolStripStatusLabel2.Text = FilesCounted.ToString()
            Application.DoEvents()
        Next curFile
End Sub
Open in New Window Select All

Answer : Treeview problem - not display folders on all drives

change this line from
AddDirectoriesNFiles(Me.TreeView1.Nodes(0), path.ToString(), "*.ttf")


to
AddDirectoriesNFiles(Me.TreeView1.Nodes(path.ToString()), path.ToString(), "*.ttf")
Random Solutions  
 
programming4us programming4us