Spurious tree nodes appearing

I have an app in which I have built a tree by searching for files of type exe on the computer.

For some reason, after displaying the tree the way I want, it then adds, spurious nodes (c:\ d:\ e:\) several times at the bottom of the tree.

I have 3 drives on my computer and all three correctly appear in the tree.

How can I get rid of these spurious nodes?
LVL 1
jindaleeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
should be something related with your tree-filling algorithm. May be helpful if you post the code.
0
jindaleeAuthor Commented:
As requested, here is the code I am using. I have also attached a jpg showing the error.

The code populates a DataGridView and a TreeView. The TreeView ends up with a bunch of nodes as shown in the attached image.
Dim theFonts As String() = New String(5) {"*.abf", "*.fnt", "*.fon", "*.shx", "*.ttc", "*.ttf"}
 
    Private Sub PerformAuditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformAuditToolStripMenuItem.Click
        ProcessIsRunning = True
        Dim fileType As String = ""
        FilesCounted = 0
        For iLoop As Integer = 0 To theFonts.GetLength(0) - 1
            fileType = theFonts(iLoop).ToString()
            dlgProgress.Text = My.Resources.searchFontFile.ToString() & fileType.ToString()
            dlgProgress.tbFileName.Text = ""
            dlgProgress.tbFilesCounted.Text = FilesCounted.ToString()
            dlgProgress.Show()
            Application.DoEvents()
            GetFiles(fileType.ToString(), dgvFonts)
        Next
        ProcessIsRunning = False
    End Sub
 
    Private Sub AuditToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuditToolStripButton1.Click
        Dim addsender As Object
        addsender = AuditToolStripButton1
        PerformAuditToolStripMenuItem_Click(AuditToolStripButton1, e)
    End Sub
 
    Private Sub GetFiles(ByVal fileMatch As String, ByVal dg As DataGridView)
        Dim path As String, i As Int32 = 0
        DisableMenus()
        'dlgProgress.Text = My.Resources.prgAudit.ToString()
 
        ' grow the tree
        Me.TreeView1.BeginUpdate()
        For i = 0 To theDrives.Length() - 1
            Try
                path = theDrives(i, 0).ToString()
                If path <> "Nothing" Then
                    Me.TreeView1.Nodes.Add(path.ToString())
                    AddDirectoriesNFiles(Me.TreeView1.Nodes(i), path.ToString(), fileMatch.ToString(), dgvFonts)
 
                End If
            Catch ex As Exception
 
            End Try
        Next
        Me.TreeView1.EndUpdate()
        dgvFonts.Sort(FileName, ComponentModel.ListSortDirection.Ascending)
        dlgProgress.Close()
        EnableMenus()
        Application.DoEvents()
    End Sub
 
    Private Sub AddDirectoriesNFiles(ByVal rootNode As TreeNode, ByVal dir As String, ByVal fileMatch As String, ByVal dg As DataGridView)
 
        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)
            Application.DoEvents()
 
            'If there are subfolders, then loop through them
            Try
                AddDirectoriesNFiles(node, di.FullName, fileMatch, dgvFonts)
                dlgProgress.ProgressBar1.PerformStep()
                dlgProgress.tbFileName.Text = di.FullName.ToString()
                If dlgProgress.ProgressBar1.Value = 100 Then
                    dlgProgress.ProgressBar1.Value = 1
                End If
                Application.DoEvents()
            Catch
 
            End Try
        Next
 
        For Each curFile As String In IO.Directory.GetFiles(dir, fileMatch)
            dlgProgress.tbFileName.Text = curFile.ToString()
            theFile = curFile.ToString()
            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)
            dlgProgress.ProgressBar1.PerformStep()
            If dlgProgress.ProgressBar1.Value = 100 Then
                dlgProgress.ProgressBar1.Value = 1
            End If
            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)
            rootNode.Nodes.Add(New TreeNode(curFile))
            dg.Rows.Add()
            dg.Rows(FilesCounted).Cells(0).Value = curFile.ToString()
            dg.Rows(FilesCounted).Cells(1).Value = dir.ToString()
            FilesCounted = FilesCounted + 1
            dlgProgress.tbFilesCounted.Text = FilesCounted.ToString()
            Application.DoEvents()
        Next curFile
    End Sub

Open in new window

tree-error.JPG
0
Jaime OlivaresSoftware ArchitectCommented:
I haven't analyzed all yet, but line 35 looks suspicious:
If path <> "Nothing" Then
maybe it is:
If path <> Nothing Then

The problem is that you scan all the folder system 6 times (one for each font extension). But every interactions creates a new set of drives.

Also, you can scan for all font file extensions:

For i = 0 To theDrives.Length() - 1
Try
path = theDrives(i, 0).ToString()
   If path <> Nothing Then
      Me.TreeView1.Nodes.Add(path.ToString())
     For Each fileMatch as String in theFonts
        AddDirectoriesNFiles(Me.TreeView1.Nodes(i), path.ToString(), fileMatch.ToString(), dgvFonts)
     End For
  End If
Catch ex As Exception

End Try
Next
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jindaleeAuthor Commented:
For each ... fixed it for me since I didn't need to scan the entire folder systenm for each font type. Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.