Enumerating File System Directories

In this blog we will learn how to display the File System Directories in TreeView and display the Files in the Directory selected.

Screenshot

File System.JPG

Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsExplorer
{
    
public partial class Form1 : Form
    {
        
TreeNode ParentNode;
        
public Form1()
        {
            InitializeComponent();
        }
        
private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ImageList = imageList1;
            
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
            {
                
if (driveInfo.IsReady)
                {
                    ParentNode = treeView1.Nodes.Add(driveInfo.Name);
                    ParentNode.ImageIndex = 0;
                    LoadDirectories(ParentNode, driveInfo.Name);
                }
            }
            treeView1.ExpandAll();
        }
        
private void LoadDirectories(TreeNode parentNode, string Path)
        {
            
TreeNode childNode;
            
DirectoryInfo dir = new DirectoryInfo(Path);
            
foreach (DirectoryInfo di in dir.GetDirectories())
            {
                childNode = parentNode.Nodes.Add(di.Name);
                childNode.ImageIndex = 1;
            }
        }
        
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
try
            {
                
if (e.Action == TreeViewAction.ByMouse)
                {
                    
DirectoryInfo dir = new DirectoryInfo(treeView1.SelectedNode.FullPath);
                    listView1.Items.Clear();
                    
foreach (FileInfo files in dir.GetFiles())
                    {
                        
ListViewItem item = new ListViewItem(files.Name);
                        item.ImageIndex = 2;
                        
this.listView1.Items.Add(item);
                    }
                }
            }
            
catch (UnauthorizedAccessException Uex)
            {
                
MessageBox.Show(Uex.Message);
            }
            
catch (System.IO.IOException ex)
            {
                
MessageBox.Show(ex.Message);
            }
        }
    }
}

Explanation

private void Form1_Load(object sender, EventArgs e)
{
    treeView1.ImageList = imageList1;
    
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
    {
        
if (driveInfo.IsReady)
        {
            ParentNode = treeView1.Nodes.Add(driveInfo.Name);
            ParentNode.ImageIndex = 0;
            LoadDirectories(ParentNode, driveInfo.Name);
        }
    }
    treeView1.ExpandAll();
}

In the form load event, we get the list of drive in the system using "DriveInfo" class and adding the drives to the treenode if the drive is ready. The Tree view's ImageList property is assigned to the Image list control which contains a collection of Images.

Then, using the LoadDirectories Method we populate the directories in the specified file system drives.

private void LoadDirectories(TreeNode parentNode, string Path)
{
    TreeNode childNode;
    
DirectoryInfo dir = new DirectoryInfo(Path);
    
foreach (DirectoryInfo di in dir.GetDirectories())
    {
        childNode = parentNode.Nodes.Add(di.Name);
        childNode.ImageIndex = 1;
    }
}

To list the files available in the selected directory I have coded the "AfterSelect" event of the Treeview Control.

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    try
    {
        
if (e.Action == TreeViewAction.ByMouse)
        {
            
DirectoryInfo dir = new DirectoryInfo(treeView1.SelectedNode.FullPath);
            listView1.Items.Clear();
            
foreach (FileInfo files in dir.GetFiles())
            {
                
ListViewItem item = new ListViewItem(files.Name);
                item.ImageIndex = 2;
                
this.listView1.Items.Add(item);
            }
        }
    }
}

Thanks for Reading.

Ebook Download
View all
Learn
View all