File System TreeView

前端 未结 5 1758
误落风尘
误落风尘 2020-11-29 10:39

Im working with file systems and I have a List<> of file objects that have the file path as a property. Basically I need to create a treeview in .NET but im struggling to

5条回答
  •  一整个雨季
    2020-11-29 11:25

    If you wanted to stick with the strings something like this would work...

    TreeNode root = new TreeNode();
    TreeNode node = root;
    treeView1.Nodes.Add(root);
    
     foreach (string filePath in myList) // myList is your list of paths
     {
        node = root;
        foreach (string pathBits in filePath.Split('/'))
        {
          node = AddNode(node, pathBits);
        }
     }
    
    
    private TreeNode AddNode(TreeNode node, string key)
    {
        if (node.Nodes.ContainsKey(key))
        {
            return node.Nodes[key];
        }
        else
        {
            return node.Nodes.Add(key, key);
        }
    }
    

提交回复
热议问题