文件系统的TreeView [英] File System TreeView
本文介绍了文件系统的TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
林与文件系统的工作,我有一个List<>具有作为一个属性的文件路径文件的对象。基本上我需要.NET创建一个TreeView,但即时通讯奋力想到去这样做,因为我需要从列表中创建一个树形结构的最佳方式,如:
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 think of the best way to go about doing this as I need to create a tree structure from a list like:
C:/WINDOWS/Temp/ErrorLog.txt
C:/Program Files/FileZilla/GPL.html
C:/Documents and Settings/Administrator/ntuser.dat.LOG
等等...
该列表不是在所有结构化的,我不能让当前的对象结构的任何变化。
The list is not structured at all and I cant make any changes to the current object structure.
我工作在C#。
非常感谢所有谁贡献
推荐答案
如果您想与琴弦坚持这样的事情会工作...
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);
}
}
这篇关于文件系统的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文