Display Directory Structure using TreeView in VB.NET

Submitted by: 
Visitors have accessed this post 2686 times.

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

So, now let's start this tutorial!

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

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 Imports System.IO to the topmost part of the code tab because we will access the directory.

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 Sub PopulateTreeView(ByVal directoryValue As String, ByVal parentNode As TreeNode)
  2. Try
  3.  
  4. Dim directoryArray As String() = Directory.GetDirectories(directoryValue)

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 Then
  2. Dim currentDirectory As String
  3.  
  4. For Each currentDirectory In directoryArray
  5. Dim myNode As TreeNode = New TreeNode(currentDirectory)
  6. parentNode.Nodes.Add(myNode)
  7. Next
  8.  
  9. End If

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

  1. Catch unauthorized As UnauthorizedAccessException
  2. parentNode.Nodes.Add("Access Denied")
  3. End Try

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. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. TreeView1.Nodes.Add("C:")
  3. PopulateTreeView("C:\", TreeView1.Nodes(0))
  4. End Sub


Full source code:

  1. Imports System.IO
  2. Public Class Form1
  3.  
  4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5. TreeView1.Nodes.Add("C:")
  6. PopulateTreeView("C:\", TreeView1.Nodes(0))
  7. End Sub
  8. Public Sub PopulateTreeView(ByVal directoryValue As String, ByVal parentNode As TreeNode)
  9. Try
  10.  
  11. Dim directoryArray As String() = Directory.GetDirectories(directoryValue)
  12.  
  13. If directoryArray.Length <> 0 Then
  14. Dim currentDirectory As String
  15.  
  16. For Each currentDirectory In directoryArray
  17. Dim myNode As TreeNode = New TreeNode(currentDirectory)
  18. parentNode.Nodes.Add(myNode)
  19. Next
  20.  
  21. End If
  22. Catch unauthorized As UnauthorizedAccessException
  23. parentNode.Nodes.Add("Access Denied")
  24. End Try
  25. End Sub
  26. End Class


Output:

output

Download the source code below and try it! :)

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
E-mail:[email protected]

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


Comments

What do you mean by that? We only do system with payments so you have to pay us for checking your source code.

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Hello
if we pay any system with source code in .net
how we know if the system not locked in future
and what the step for check source code

thanks

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.