List Drives and Folders in a TreeView Using C#

June 4, 2013 | By

The .NET Framework makes it easy to read drive and folder details but it does not include a Windows Forms control to automatically list them other than the FolderBrowserDialog control. Here is a fast and simple way to use a TreeView control to list the drives and folders in a system.

Listing folders in C#

The trick to keeping the TreeView control responsive and prevent it from hanging is to read and display the drives only on the form load event. Then read and display the folders the user expands. This way the user will not have to wait for the application to read all or most of the folder structure before using it. To accomplish that every node that represents a drive or a folder should have its path saved in the TreeNode’s Tag property.

The TreeView’s BeforeExpand event occurs immediately after the user clicks the TreeNode’s plus sign to expand it and show its children. Here we just get a list of the folders in the path that is the Tag property and add a TreeNode for each.

If you found this post useful, I’d be very grateful if you’d help it spread by sharing it. Thank you!

 

Download Source Code

 

Filed in: WinForms | Tags: , ,

Comments (11)

  1. Craig

    Hello Amgad, I have a project on git hub ( https://github.com/cgleadall/CAAB ) that will basically allow you to copy “all files” from a folder to an external drive – CAAB stands for Copy As A Backup.
    Sometimes you just want to make sure that you have a copy of stuff.
    I would love if you could ‘pull request’ this treeview solution into that project. We could even then add checkboxes on the folders so you can “unselect” one or two folders (and subfolders).

    Thanks for this post.

    P.S. I found the post through Coderwall’s Pro Tips.

    Craig.

  2. Ralph Freshour

    Where are the images stored? I’m new to VS 2012 and C#. I don’t see where you are storing your disk images?

    Thanks…

  3. Wenz

    Thanks for the post.

  4. Reyansh Jain

    grt work….!!!! thanks for the post…. and u r requested to share post like this……..

  5. Ali

    This is the before expand code
    private void dirsTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)

    I want to ask what will be the code of AfterSelect because when I Expand the drives there are no file or folder
    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {

    }
    Please Tell me

  6. Ali, I didn’t get your question. The code in the BeforeExpand event should be enough to list the files in the folder you expanded. Does it not display the list of files in the folder?

  7. Ali

    Bro I makes an application in which when I double clicked the treeview it goes to that function in Form1.cs file

    private void dirsTreeView_AfterSelect(object sender, TreeViewEventArgs e)
    {

    }
    I also paste your code as u mention how to add that code the program is running correctly but when I clicked on plus sign of any drive it shows nothing..
    Help me What can I do….

  8. Make sure the treeview triggers BeforeExpand events. You can check that by viewing the it’s events in the properties page.

  9. Ali

    Bro Can you provide me complete project(source code) in a files..So I can run that because there are issues in it..I cant understand what to do So Please Help me

  10. Ali, you can now download the C# project using link at then of the article.

  11. Ali

    Thanx Amgad Suliman for helping me..One question more that if I want to open any file from that list. what will be the code needed for that can you provide me that code..