View files in a TreeView

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
4 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

View files in a TreeView
MrAksel
This tutorial will show you how to view files in a TreeView. You do not need to add anything but a TreeView to your form, but remember to import System.IO in your code.
In your Form_Load event, we will load the logical drives on the computer so we can browse through the files.
Code: Select all
        For Each Drive As DriveInfo In DriveInfo.GetDrives()
            If Drive.IsReady Then
                Dim node As New TreeNode(Drive.Name)
                If Not String.IsNullOrEmpty(Drive.VolumeLabel) Then node.Text &= " - " & Drive.VolumeLabel

                node.Tag = Drive.RootDirectory
                node.Nodes.Add("*")
                TreeView1.Nodes.Add(node)
            End If
        Next
This loops through each drive that it can access, and checks if it is ready to use before continuing. A new TreeNode is created with the name of the drive. If it also has a label like "System" or "Packard Bell" it will be added to the text of the node.
Then it sets the Tag property of the TreeNode to the root directory of the drive. This way we can quickly see the contents later.
In the end we add the new TreeNode to our TreeView.

Whenever we expand a node, the event BeforeExpand is raised. In this event, we will put our code to load directories and files into the TreeView.
Code: Select all
 If Not e.Node.FirstNode.Text = "*" Then Return

        e.Node.Nodes.Clear()
        Dim directory As DirectoryInfo = CType(e.Node.Tag, DirectoryInfo)
        
If the the first node is not "*", the directory has either already been loaded, or does not contain any sub items, so return.
If the directory has not previously been loaded, remove the "*" node and get the store the directory of the node in a variable we call 'directory'.

Next up is to load the directories
Code: Select all
        Try
            For Each d As DirectoryInfo In directory.GetDirectories()
                Dim node As New TreeNode(d.Name)
                node.Tag = d
                If d.GetDirectories().Count + d.GetFiles().Count > 0 Then
                    node.Nodes.Add("*")
                End If
                e.Node.Nodes.Add(node)
            Next
        Catch ex As Exception
            MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
        End Try
Here we loop through every sub-directory and create a new node, with the Tag set to the directory the node represents. If the directory contains files or other subdirectories, add "*" to mark that. After we have set up our node, add it to the parent node.
If something went wrong we display the error.

Now we need to loop through our files in the directory
Code: Select all
        Try
            For Each d As FileInfo In directory.GetFiles()
                Dim node As New TreeNode(d.Name)
                e.Node.Nodes.Add(node)
            Next
        Catch ex As Exception
            MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
        End Try
This is a bit easier code because a file cannot contain subdirectories or files. Here we only have to create a node with the name of the file and add it to the TreeView. If an error occurred, display it.

Your full code should now look like this
Code: Select all
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        For Each Drive As DriveInfo In DriveInfo.GetDrives()
            If Drive.IsReady Then
                Dim node As New TreeNode(Drive.Name)
                If Not String.IsNullOrEmpty(Drive.VolumeLabel) Then node.Text &= " - " & Drive.VolumeLabel

                node.Tag = Drive.RootDirectory
                node.Nodes.Add("*")
                TreeView1.Nodes.Add(node)
            End If
        Next
    End Sub

    Private Sub TreeView1_BeforeExpand(sender As System.Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        If Not e.Node.FirstNode.Text = "*" Then Return

        e.Node.Nodes.Clear()
        Dim directory As DirectoryInfo = CType(e.Node.Tag, DirectoryInfo)

        Try
            For Each d As DirectoryInfo In directory.GetDirectories()
                Dim node As New TreeNode(d.Name)
                node.Tag = d
                If d.GetDirectories().Count + d.GetFiles().Count > 0 Then
                    node.Nodes.Add("*")
                End If
                e.Node.Nodes.Add(node)
            Next
        Catch ex As Exception
            MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
        End Try

        Try
            For Each d As FileInfo In directory.GetFiles()
                Dim node As New TreeNode(d.Name)
                e.Node.Nodes.Add(node)
            Next
        Catch ex As Exception
            MessageBox.Show("Error - " & ex.GetType().FullName & vbNewLine & ex.Message)
        End Try
    End Sub
End Class
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Re: View files in a TreeView
Skillful
Nice tutorial MrAksel! :)
Keep it up! cooll;
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: View files in a TreeView
Shim
nice tut ;-)
Find my programs on Softpedia
User avatar
RonaldHarvey
Top Poster
Top Poster
Posts: 113
Joined: Tue Apr 05, 2011 2:32 pm

Re: View files in a TreeView
RonaldHarvey
Thanks my friend this is a good tutorial. cooll; cooll;
4 posts Page 1 of 1
Return to “Tutorials”