TreeView Iteration


New member
Apr 3, 2015
Programming Experience
I'm trying to build a tree view off this old menu system and need to figure out the best way of doing this.

Basically I will read in the MAIN.TXT file below and those will be my high level nodes, but sub menus exist in MAIN.TXT like Submenu.txt and SecondSubmenu.txt, those will be separate text files.

Now my best guess here is that I should load in MAIN.txt first, then iterate through that list, and then iterate through the sub menus. The problem I am having is how would I structure my iteration to perform this?

Sample text files:

A|runme.bat|Main Execution
A|runthis.bat|Secondary Execution
A|runagain.bat|Tietary Execution

A|Test.exe|Test Executable

A|Secondmenu.bat|Second Menu batch

A|Thirdmenu.bat|Third Menu batch

What I want my output to be:

runme.bat - main execution
runthis.bat - secondardy execution
Submenu.cfg - SubMenu
     Test.exe - Test Executable
runagain.bat - tietary execution
     Secondmenu.bat - Second Menu batch
    Thirdmenu.txt - thirdSubMenu
     thirdmenu.bat - Third Menu batch
Last edited:
This is obviously not a general question. A TreeView is a control so the question is specific to the UI technology in use. Is it a Windows Forms TreeView or WPF or Web Forms? Once I know that, I can move this thread to the proper forum and provide a technology-appropriate response.
Any tree structure is inherently recursive so it's almost always preferable to use recursion to traverse a TreeView. In the case of the WinForms TreeView, the control itself has a Nodes property that is type TreeNodeCollection and so does every TreeNode, so that is the place to recurse from. In your case, you'll want a method something like this:
private TreeNode[] PopulateNodesFromFile(TreeNodeCollection nodes, string filePath)
    var childNodes = new List<TreeNode>();

    foreach (var line in File.ReadLines(filePath))
        var node = new TreeNode();

        // Configure node based on line.

        // If line contains child file name, recurse to populate child nodes.
        PopulateNodesFromFile(node.Nodes, "child file path here");


You'll start the ball rolling by calling that method and passing the TreeView's own Nodes collection and the path of the main file.
Top Bottom