public void PopulateTreeView(string directoryValue, TreeNode parentNode)
{
string[] directoryArray = Directory.GetDirectories(directoryValue);
try
{
if (directoryArray.Length != 0)
{
foreach (string directory in directoryArray)
{
TreeNode myNode = new TreeNode(directory);
parentNode.Nodes.Add(myNode);
PopulateTreeView(directory, myNode);
}
}
}
catch (UnauthorizedAccessException)
{
parentNode.Nodes.Add("Access denied");
}
}
private void treeView1_Load(object sender,EventArgs e)
{
treeView1.Nodes.Add("g:\\");
PopulateTreeView("g:\\", treeView1.Nodes[0]);
}
用递归的方法来实现