首页 > 编程 > C# > 正文

C# 递归查找树状目录实现方法

2020-01-24 02:48:26
字体:
来源:转载
供稿:网友

1.递归查找树状目录

复制代码 代码如下:

 public partial class Form1 : Form
    {
        string path = @"F:/学习文件";//递归查找树状目录
        public Form1()
        {递归查找树状目录
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadTree(path);
        }

 public void LoadTree(string path, TreeNode node=null)

        {           

string[] dirs = Directory.GetDirectories(path);//获取子目录

            foreach (string dir in dirs)

            {

                  TreeNode node1 = new TreeNode(Path.GetFileName(dir));

                //TreeNode node1 = new TreeNode(dir);//文件所有路径

                if (node == null)

                {

                    treeView1.Nodes.Add(node1);

                }

                else

                {

                    node.Nodes.Add(node1);

                }

 if (Directory.GetDirectories(dir).Length > 0)
                {
                    LoadTree(dir, node1);
                }
            }
        }
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表