Show Drives With Folders and Files in TreeView in VB.Net, C#

If you are trying to show Drives, Folders, Sub-Folders and Files without using FolderBrowserDialog or OpenFileDialog through VB.Net Code. Then you can try TreeView control for this process. To use TreeView as File Explorer or Folder Explorer, there are some simple and easy coding and setting tips you have to apply.

So, here we try to Show Drives with Folders and Files in TreeView in VB.Net, C# coding.

(File's Icons in ListView) Show List of Files with Files' Icons in Listview

But, first we understand why we need TreeView as FileExplorer? The answer is very simple, whenever we develop1 an application and want to show files, folders or drives in static way then we use many controls like ListBox, ListView or TreeView as File browser or Folder browser.

Now, Lets proceed with following point:

  1. Show Drives with Folders and File in TreeView in VB.Net (VB, C#)
  2. Get TreeView Selected Node Path in Label VB.Net (VB, C#)

Now, let's start with point wise.

Searching Files from Folders or Sub-Folders through code using VB.Net

(1). Show Drives with Folders and File in TreeView in VB.Net (VB , C#) :

First we have to add some controls like Button, Label, Treeview. Now here we use these controls with following Names.

ExplorerBtn : Button
MytrView1 : Treeview
MyPathLabel : Label

First, we write code inside Button for calling Drives.

Button’s Click Event for Showing Drives in TreeView.

VB.Net :
 Private Sub ExplorerBtn_Click(sender As Object, e As EventArgs)
  'Clear TreeView
  MytrView1.Nodes.Clear()

  'Loop For Get Drives
  For Each myDrives As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives()
     Dim myDrivesNode As TreeNode = MytrView1.Nodes.Add(mydrives.Name)
     'Adding "Expand" string is use for Add Expand "[+]" option on Drives
     myDrivesNode.Nodes.Add("Expand")
  Next

 End Sub

C# :
 private void ExplorerBtn_Click(object sender, EventArgs e)
 {
    //Clear TreeView
    MytrView1.Nodes.Clear();

    //Loop For Get Drives
    foreach (System.IO.DriveInfo myDrives in System.IO.DriveInfo.GetDrives())
    {
       TreeNode myDrivesNode = MytrView1.Nodes.Add(myDrives.Name);
       //Adding "Expand" string is use for Add Expand "[+]" option on Drives
       myDrivesNode.Nodes.Add("Expand");
    }

  }

So, this code will present output in following way:
Show Drives in Treeview VB.Net
After that, we use 'BeforeExpand' event of Treeview for getting Folders and Files List on particular Drives.

VB.Net :
 Private Sub MytrView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs)
   Dim MyExistNode As TreeNode = e.Node
   'Clear TreeNode
   MyExistNode.Nodes.Clear()

   Try
     'Loop For Get Folders
     Dim mypath As String = MyExistNode.FullPath

     'Loop For Get Folders
     For Each myFolders As String In System.IO.Directory.GetDirectories(mypath)
        Dim FldrNode As TreeNode = MyExistNode.Nodes.Add(System.IO.Path.GetFileName(myFolders))
        'Here, Expand is use for add Expanding option "[+]" on folder
        FldrNode.Nodes.Add("Expand")
     Next

     'Loop For Get Files
     For Each MyFiles As String In System.IO.Directory.GetFiles(mypath)
        Dim FLNode As TreeNode = MyExistNode.Nodes.Add(System.IO.Path.GetFileName(MyFiles))
     Next

    Catch ex As Exception

    End Try
  
 End Sub

C# :
private void MytrView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  {
     TreeNode MyExistNode = e.Node;
     //Clear TreeNode
     MyExistNode.Nodes.Clear();

     try
      {
        string mypath = MyExistNode.FullPath;

        //Loop For Get Folders
        foreach (string myFolders in System.IO.Directory.GetDirectories(mypath))
        {
           TreeNode FldrNode = MyExistNode.Nodes.Add(System.IO.Path.GetFileName(myFolders));
           //Here, Expand is use for add Expanding option "[+]" on folder
           FldrNode.Nodes.Add("Expand");
         }

        //Loop For Get Files
        foreach (string MyFiles in System.IO.Directory.GetFiles(mypath))
         {
                
            TreeNode FLNode = MyExistNode.Nodes.Add(System.IO.Path.GetFileName(MyFiles));
               
          }

       }
        catch (Exception FlErr)
       {
          MessageBox.Show(FlErr.ToString());
       }

   }

OK, when you run your application this will show following output:
Show Drives Folders Files in Treeview VB.Net

As you can see, when you click on any drive this will expand with containing Folders and files and when you expand any folder this will show your list of sub-folders and files in Treeview.

Styling ProgressBar in VB.Net

(2). Get Treeview Selected Node Path in Label VB.Net (VB, C#) :

Now, let’s try to get selected Node path in Label from Treeview. So, here we go through code process.

You have to first go to “AfterSelect” event of Treeview and write following code:

VB.Net:
 Private Sub MytrView1_AfterSelect(sender As Object, e As TreeViewEventArgs)
    
  Dim MySelectedNode As TreeNode = e.Node
  'Get Selected Node Path
  MyPathLabel.Text = MySelectedNode.FullPath
    
 End Sub

C# :
private void MytrView1_AfterSelect(object sender, TreeViewEventArgs e)
  {
     TreeNode MySelectedNode = e.Node;
     //Get Selected Node Path
     MyPathLabel.Text = MySelectedNode.FullPath;
    
  }

This will work like following way:
Get TreeView Selected Node Path in Label VB.Net

Okay, now we have learned how to Show Drives with Folders and Files in TreeView in VB.Net using VB and C# code. You can also try and ask any question related to this topic describe in SKOTechLearn Tips.

0 comments: