Display Directory Structure using TreeView in C#

Submitted by: 
Language: 
Visitors have accessed this post 1193 times.

Today in C#, i will teach you on how to create a program that loads and displays the directory structure of C using the TreeView control in C#.

So, now let's start this tutorial!

1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application and named your project as TreeViewDirectory.

2. Next, add only one TreeView1 named TreeView11 to display the contents of C directory structure. You must design your interface like this:

design

3. Add Using System.IO to the topmost part of the code tab because we will access the directory.

  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10. using System.IO;

4. Create a method named PopulateTreeView with directoryValue As String and parentNode As TreeNode as parameters.

On this, create a try and catch method. In the try method, create an array variable (string) named directoryArray that holds and gets the directory either C or D.

  1. public void PopulateTreeView(string directoryValue, TreeNode parentNode)
  2. {
  3. try
  4. {
  5.  
  6. string[] directoryArray = Directory.GetDirectories(directoryValue);
  7.  

Now, create an If statement that if your computer has obtained a directory this will add all the directory folders to the treeview using For Next Loop.

  1. if (directoryArray.Length != 0)
  2. {
  3. string currentDirectory = default(string);
  4.  
  5. foreach (string tempLoopVar_currentDirectory in directoryArray)
  6. {
  7. currentDirectory = tempLoopVar_currentDirectory;
  8. TreeNode myNode = new TreeNode(currentDirectory);
  9. parentNode.Nodes.Add(myNode);
  10. }
  11.  
  12. }
  13. }

In the catch method, put this code below to handle UnauthorizedAccessException and will prompt the user "Access Denied".

  1. catch (UnauthorizedAccessException)
  2. {
  3. parentNode.Nodes.Add("Access Denied");
  4. }

5. Add this code for the form_load of your program. This will load the C directory structure using the PopulateTreeView method that we have created above.

  1. public void Form1_Load(System.Object sender, System.EventArgs e)
  2. {
  3. TreeView1.Nodes.Add("C:");
  4. PopulateTreeView("C:\\", TreeView1.Nodes[0]);
  5. }
  6. pub

Full source code:

  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10. using System.IO;
  11.  
  12. namespace TreeViewDirectory
  13. {
  14. public partial class Form1
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19.  
  20. }
  21.  
  22.  
  23. public void Form1_Load(System.Object sender, System.EventArgs e)
  24. {
  25. TreeView1.Nodes.Add("C:");
  26. PopulateTreeView("C:\\", TreeView1.Nodes[0]);
  27. }
  28. public void PopulateTreeView(string directoryValue, TreeNode parentNode)
  29. {
  30. try
  31. {
  32.  
  33. string[] directoryArray = Directory.GetDirectories(directoryValue);
  34.  
  35. if (directoryArray.Length != 0)
  36. {
  37. string currentDirectory = default(string);
  38.  
  39. foreach (string tempLoopVar_currentDirectory in directoryArray)
  40. {
  41. currentDirectory = tempLoopVar_currentDirectory;
  42. TreeNode myNode = new TreeNode(currentDirectory);
  43. parentNode.Nodes.Add(myNode);
  44. }
  45.  
  46. }
  47. }
  48. catch (UnauthorizedAccessException)
  49. {
  50. parentNode.Nodes.Add("Access Denied");
  51. }
  52. }
  53. }
  54.  
  55. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.