转自:http://www.jb51.net/article/46821.htm
这篇文章主要介绍了C# 递归查找树状目录实现方法,需要的朋友可以参考下
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);
- }
- }
- }
- }
- }
复制代码
|
|