'C# Creating Binary Tree Treeview from folders and files

I am trying to create my own treeview binary tree where it shows folders and children / parents for certain folders. I have a big list of folders then I have it in a custom object the Index, Parent Name, Children bool, and display name on the tree. I can create the tree but when it creates it all the children are under the first node and not under the correct ones. I see that treenode has level and parent if I could set those then this would be easy but I see they are readonly so I’m kinda stuck so far…

                List<TreeNodeEnd> lstTreeNod = new List<TreeNodeEnd>();
                Dictionary<int, TreeNode> valuePairs = new Dictionary<int, TreeNode>();
                        foreach (var node in lstTreeNod)
                        {
                            TreeNodeCollection items;
                            TreeNode treeNode;
                            if (node.TreeIndex == 0)
                            {
                                //node.DisplayName = "PDM Vault";
                                //TreeNode root2 = new TreeNode("PDM Vault", 3, 3, items);
                                //root.Name = "PDM Vault";
                                items = treeView1.Nodes;
                                //items = items.Add(node.DisplayName);
                                //treeView1.Nodes
                            }
                            else  //in this ELSE DETERMINE ABOUT FATHER & CHILDREN EQUALS AND NOT NULL
                            {       // ValuePairs = Final Tree ?

                                //items = valuePairs[node.TreeIndex - 1].Nodes;

                                items = valuePairs[node.TreeIndex].Nodes;

                                TreeNode treeNode = items.Add(node.DisplayName);


                            }

                            TreeNode treeNode = items.Add(node.DisplayName);

                            if (valuePairs.ContainsKey(node.TreeIndex))
                            {
                                if (node.HasChildren == true)
                                    valuePairs[node.TreeIndex] = treeNode;
                            }
                            else
                            {
                                valuePairs.Add(node.TreeIndex, treeNode);
                            }

}

public class TreeNodeEnd
{
    public string DisplayName { get; set; }
    public int TreeIndex { get; set; }
    public bool HasChildren { get; set; }
    public string Father { get; set; }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source