c#构建目录树

时间:2018-05-14 13:48:47

标签: c# tree directory-structure ext.net

我正在尝试在我的应用程序中构建目录树。 我在Ext.NET使用此Example。 如何在给定路径中搜索目录和子目录以构建此树?

以下是示例中给出的用于从节点构建树的代码:

protected void NodeLoad(object sender, NodeLoadEventArgs e)
    {

        if (!string.IsNullOrEmpty(e.NodeID))
        {
            for (int i = 1; i < 6; i++)
            {
                AsyncTreeNode asyncNode = new AsyncTreeNode();
                asyncNode.Text = e.NodeID + i;
                asyncNode.NodeID = e.NodeID + i;
                e.Nodes.Add(asyncNode);
            }

            for (int i = 6; i < 11; i++)
            {
                Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();
                treeNode.Text = e.NodeID + i;
                treeNode.NodeID = e.NodeID + i;
                treeNode.Leaf = true;
                e.Nodes.Add(treeNode);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

这样的事情:

<script runat="server">
    protected void NodeLoad(object sender, NodeLoadEventArgs e)
    {
        // NodeID stores the local disk path ( full path ) of the selected node to expand
        string path = e.NodeID;

        if (!string.IsNullOrEmpty(e.NodeID))
        {
            foreach(var folder in System.IO.Directory.GetDirectories(path))
            {
                string folderName = new System.IO.DirectoryInfo(folder).Name;
                Node folderNode = new Node();
                folderNode.Text = folderName;
                folderNode.NodeID = folder;
                e.Nodes.Add(folderNode);
            }

            foreach(var fileLeaf in System.IO.Directory.GetFiles(path))
            {
                Node treeNode = new Node();
                treeNode.Text = System.IO.Path.GetFileName(fileLeaf);
                treeNode.NodeID = fileLeaf;
                treeNode.Leaf = true;
                e.Nodes.Add(treeNode);
            }
        }
    }
</script>

对我来说,我只使用C:\作为示例,您需要将根文件夹放在元素中:

<Root>
    <ext:Node NodeID="C:\" Text="C:\"/>
</Root>