Skip to main content

Display Directory Structure using TreeView in C#


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 R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

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

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.