C recursive search tree directory implementation method
- 2020-06-12 10:31:34
- OfStack
1. Recursively find the tree directory
public partial class Form1 : Form
{
string path = @"F:\ Study documents ";// Recursively find the tree directory
public Form1()
{ Recursively find the tree directory
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadTree(path);
}
public void LoadTree(string path, TreeNode node=null)
{
string[] dirs = Directory.GetDirectories(path);// Get subdirectories
foreach (string dir in dirs)
{
TreeNode node1 = new TreeNode(Path.GetFileName(dir));
//TreeNode node1 = new TreeNode(dir);// All file paths
if (node == null)
{
treeView1.Nodes.Add(node1);
}
else
{
node.Nodes.Add(node1);
}
if (Directory.GetDirectories(dir).Length > 0)
{
LoadTree(dir, node1);
}
}
}
}
}