C#使用n深度的文件填充treeView

时间:2013-06-27 17:20:02

标签: c# treeview populate

我不知道如何在树视图控件中显示所有数据: 这是我的代码:

 private void PopulateTree(string path, int depth, TreeNode parent)
    {
        if (depth == 0)
        {
        //This make a child
            parent.Nodes.Add(new TreeNode(path);
            return;
        }
        //This makes a parent
        TreeNode first = new TreeNode(path);
        parent.Nodes.Add(first);

        foreach (var v in ListWithPaths)
        {
            PopulateTree(v, depth - 1, first);
        }
    }

它似乎只在depth=1

时有用
parent
-parent
--parent
---child
---child
--parent
---child
---child
-/parent
/parent

这就是我的看法......

2 个答案:

答案 0 :(得分:0)

我从here找到了这个答案:

private void Form1_Load(object sender, EventArgs e)
    {
        var paths = new List<string>
                        {
                            @"C:\WINDOWS\AppPatch\MUI\040C",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
                            @"C:\WINDOWS\addins",
                            @"C:\WINDOWS\AppPatch",
                            @"C:\WINDOWS\AppPatch\MUI",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
                        };

        treeView1.PathSeparator = @"\";

        PopulateTreeView(treeView1, paths, '\\');
}


private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
    {
        TreeNode lastNode = null;
        string subPathAgg;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(pathSeparator))
            {
                subPathAgg += subPath + pathSeparator;
                TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                if (nodes.Length == 0)
                    if (lastNode == null)
                        lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                else
                    lastNode = nodes[0];
            }
        }
    }

        foreach (var v in ListWithPaths)
        {
            PopulateTree(v, depth - 1, first);
        }
    }

基于路径,它创造了深度。 如果这不是所希望的行为,您可以相应地更改PopulateTreeView - 功能。

答案 1 :(得分:0)

您的代码中有一些不明确的时刻:

  1. 是否要明确限制路径深度(例如,仅显示高达3d级别的路径)
  2. 什么是ListWithPaths?两条路径可以有一个公共节点吗?
  3. 要显示具有限制(深度)的单个路径,代码可以是

    private void PopulateTree(String path, TreeNode parent, int depth) {
      if (depth == 0) // <- Artificial criterium
        return;
    
      if (String.IsNullOrEmpty(path))
        return;
    
      int index = path.IndexOf(Path.DirectorySeparatorChar);
    
      String directoryName = (index < 0) ? path : path.Substring(0, index);
      String otherName = (index < 0) ? null : path.Substring(index + 1);
    
      TreeNode childNode = parent.Nodes.Add(directoryName);
    
      PopulateTree(otherName, childNode, depth - 1);
    }
    

    为了加载路径集合而没有任何限制和可能的公共节点,您可以使用 像这样的东西:

    private void PopulateTree(String path, TreeView view, TreeNode parent) {
      if (String.IsNullOrEmpty(path))
        return;
    
      int index = path.IndexOf(Path.DirectorySeparatorChar);
    
      String directoryName = (index < 0) ? path : path.Substring(0, index);
      String otherName = (index < 0) ? null : path.Substring(index + 1);
    
      TreeNode childNode = null;
      TreeNodeCollection nodes = (parent == null) ? view.Nodes : parent.Nodes;
    
      foreach (TreeNode node in nodes)
        if (String.Equals(node.Name, directoryName)) {
          childNode = node;
    
          break;
        }
    
      if (childNode == null)
        childNode = nodes.Add(directoryName);
    
      PopulateTree(otherName, view, childNode);
    }
    
    private void PopulateTree(IEnumerable<String> paths, TreeView view) {
      view.BeginUpdate();
    
      try {
        foreach (String path in paths)
          PopulateTree(path, view, null);
      }
      finally {
        view.EndUpdate();
      }
    }
    
    ...
    
    PopulateTree(ListWithPaths, MyTreeView)