[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Spurious tree nodes appearing

Posted on 2009-07-03
4
Medium Priority
?
259 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?
0
Comment
Question by:jindalee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24772238
should be something related with your tree-filling algorithm. May be helpful if you post the code.
0
 
LVL 1

Author Comment

by:jindalee
ID: 24775012
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 24775072
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
 
LVL 1

Author Closing Comment

by:jindalee
ID: 31599537
For each ... fixed it for me since I didn't need to scan the entire folder systenm for each font type. Many thanks
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question