We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Spurious tree nodes appearing

Medium Priority
284 Views
Last Modified: 2013-11-27
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?
Comment
Watch Question

Jaime OlivaresSoftware Architect
CERTIFIED EXPERT
Top Expert 2008

Commented:
should be something related with your tree-filling algorithm. May be helpful if you post the code.

Author

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
Software Architect
CERTIFIED EXPERT
Top Expert 2008
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
For each ... fixed it for me since I didn't need to scan the entire folder systenm for each font type. Many thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.