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);
}
}
}
}
}
最新评论