C#用n深度的文件填充treeView
本文关键字:文件 填充 treeView 深度 | 更新日期: 2023-09-27 18:20:14
我不知道如何在树视图控件中显示所有数据:这是我的代码:
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
这就是我的看法…
我从这里找到了这个答案:
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
-函数。
您的代码中有一些不清楚的时刻:
- 是否要明确限制路径深度(例如,仅显示三维级别的路径)
- 什么是ListWithPaths?两条路径可以有一个公共节点吗
为了显示具有限制(深度)的单个路径,代码可以是
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)