c#构建目录树

c# Build a directory tree

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

这是示例中给出的从节点构建树的代码:

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

像这样:

<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>