TreeView Structure
Hi,
I want to show the directory structure in a treeview manner, how to do that. eg:
-Desktop
+ My Documents
-My Computer
- Local Disk(C:)
please advice
regards,
bjb
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 7, 2009, 6:51 AM EDT
Hi , Here is code How Can you Do that private void button1_Click(object sender, EventArgs e) { //Here i just populated C:\ using Code But you can also iterate through all //Subfolders to add in tree using Same function with Foreach treeView1.Nodes.Add(@"C:"); PopulateTreeView(@"C:\", treeView1.Nodes[0]); }public void PopulateTreeView(string directoryValue, TreeNode parentNode) { try { string[] directoryArray = Directory.GetDirectories(directoryValue); if (directoryArray.Length != 0) { string currentDirectory = ""; foreach (string dirinfo in directoryArray) { TreeNode myNode = new TreeNode(dirinfo); parentNode.Nodes.Add(myNode); } // PopulateTreeView(currentDirectory, myNode) } } catch { } Dont forget to mark "Do you like Answer" please it will give me some credits :) Thank you :)Roei BarRank #1,271 · 1.4K reputationPosted Aug 7, 2009, 8:07 AM EDT
b b47 reputationPosted Aug 7, 2009, 10:37 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 8, 2009, 12:02 AM EDT
Please mark "Do you like answer" it will give me some credits
Download this Class File From Below Link Add it in Your Project and
http://www.mediafire.com/?wqvi21mjlww
Use it like Below Code by by creating its instance
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e) { if (e.Node.Nodes[0].Text == "") { TreeNode node = fe.EnumerateDirectory(e.Node); } }b b47 reputationPosted Aug 9, 2009, 10:14 PM EDT
Kirtan PatelRank #292 · 8.6K reputationPosted Aug 10, 2009, 5:16 AM EDT
Comment Out Following lines in it Rerun the project you will get what you want :)
Dont Forget to Mark "Do you Like Answer" please it will give me some credits :)
//Fill files //foreach (FileInfo file in rootDir.GetFiles()) //{ // TreeNode node = new TreeNode(); // node.Text = file.Name; // node.ImageIndex = 2; // node.SelectedImageIndex = 2; // parentNode.Nodes.Add(node); //}