文件系统树视图 [英] File System TreeView

查看:25
本文介绍了文件系统树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用文件系统,并且我有一个以文件路径作为属性的文件对象的 List<>.基本上我需要在 .NET 中创建一个树视图,但我正在努力想出最好的方法来做到这一点,因为我需要从如下列表中创建一个树结构:

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);
    }
}

这篇关于文件系统树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆