TreeView - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

TreeView

SlothmanSlothman Posts: 25Member
[b][red]This message was edited by Slothman at 2006-5-30 3:46:32[/red][/b][hr]
Hi, I got the following code that adds any directory I choose from a folderbrowsedialog. It ads all the subdirectories of the directory I chose but how do I display all directories subdirectories?
For example:

C:
documents and Settings <--- How to get all directories and subdirs?
Program Files
windows

If Dir.ShowDialog = DialogResult.OK Then
Tree.Nodes.Clear()
Dim strPath As String = Dir.SelectedPath
Dim diDirectories As New DirectoryInfo(strPath)
Dim directory() As DirectoryInfo

Try
directory = diDirectories.GetDirectories()
Catch ex As Exception
Exit Sub
End Try

Dim di As DirectoryInfo
Dim tn As New TreeNode
tn.Text = strPath
For Each di In directory
tn.Nodes.Add(di.Name)
Next
Tree.Nodes.Add(tn)
End If


Comments

  • HackmanCHackmanC Posts: 441Member
    [b][red]This message was edited by HackmanC at 2006-5-31 13:31:18[/red][/b][hr]
    Use recursion ... I will explain only the algorith, you can fill the blanks with the code you already done.

    [code]
    function theMainExecution()
    TreeNode n = new TreeNode("C:") ' GET THE ROOT NODE
    myTreeView.Nodes.Add(n)
    DirectoryInfo dirInfo = new DirectoryInfo("C:") ' CREATE A NEW dirInfo WITH ROOT DRIVE
    recursiveTreeViewAdd(n, dirInfo) ' CALL THE RECURSIVE FUNCTION
    end function

    function recursiveTreeViewAdd(actualNode as TreeViewNode, dirInfo as DirectoryInfo)
    theNewDirecotires = dirInfo.getDirectories()
    For Each d In theNewDirectories
    TreeViewNode n = actualNode.Add(di.Name) ' ADD A NEW NODE INTO THE PARAMETER actualNode
    recursiveTreeViewAdd(n, d) ' CALL THIS FUNCTION AGAIN UNTIL THERE ARE NO MORE DIRECTORIES INSIDE
    Next
    end function
    [/code]

    This function do not work if you try to cut and paste, you should fix the syntax, and the SOME of the logic.

    ----
    : [b][red]This message was edited by Slothman at 2006-5-30 3:46:32[/red][/b][hr]
    : Hi, I got the following code that adds any directory I choose from a folderbrowsedialog. It ads all the subdirectories of the directory I chose but how do I display all directories subdirectories?
    : For example:
    :
    : C:
    : documents and Settings <--- How to get all directories and subdirs?
    : Program Files
    : windows
    :
    : If Dir.ShowDialog = DialogResult.OK Then
    : Tree.Nodes.Clear()
    : Dim strPath As String = Dir.SelectedPath
    : Dim diDirectories As New DirectoryInfo(strPath)
    : Dim directory() As DirectoryInfo
    :
    : Try
    : directory = diDirectories.GetDirectories()
    : Catch ex As Exception
    : Exit Sub
    : End Try
    :
    : Dim di As DirectoryInfo
    : Dim tn As New TreeNode
    : tn.Text = strPath
    : For Each di In directory
    : tn.Nodes.Add(di.Name)
    : Next
    : Tree.Nodes.Add(tn)
    : End If
    :
    :
    :

    [red]Good luck![/red]
    [blue]Hackman[/blue]

  • SlothmanSlothman Posts: 25Member
    Thx HackmanC. I tried what you explained. Works perfectly.
    Appreciate your help.
Sign In or Register to comment.