Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
am trying to to populate a tree view to display all folders on my computer using the C# language
i have try many different ways but none worked
can any one please help me out
Posted
Comments
Sergey Alexandrovich Kryukov 21-May-13 21:27pm    
What a useless post! Not even a question. You cannot expect any help. We have no idea what's your problem (except probably laziness, even in asking questions).
—SA

1 solution

Hi Sean,
You can use
string[] drives=Environment.GetLogicalDrives() to get all the Drives from your computer. and then you can use
string[] folders = Directory.GetDirectories(path); to get all the folders to the respective Path.

U can try the following code

//
string[] driveList = Environment.GetLogicalDrives();
TreeNode treeNodeParent = new TreeNode();
TreeNode treeNodeChd = new TreeNode();
foreach(string s in driveList)
{
treeNodeChd = new TreeNode();

treeNodeChd = getDirectories(s);
treeNodeChd.Text = s;
treeNodeParent.ChildNodes.Add(treeNodeChd);
}
TreeView1.Nodes.Add(treeNodeParent);

//Function to call to get folders in each Directory
public TreeNode getDirectories(string path)
{
string[] dirList = Directory.GetDirectories(path);
TreeNode treeNode = new TreeNode();
foreach (string s in dirList)
{
TreeNode treeChildNode = new TreeNode();

treeChildNode = getDirectories(s);
treeChildNode.Text = s;
treeNode.ChildNodes.Add(treeChildNode);
}
return treeNode;
}

Hope it is helpful..
 
Share this answer
 
Comments
sean871 21-May-13 21:52pm    
thxs alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900