Display all Files and Folders in a Directory in C#


You can easily list and display all the files and folders in a C# application using the DirectoryInfo class. First have a look at the application screenshot which uses this class to display the Files and Folders in a specified directory. All the Files and Folders are displayed in a ListView



An object created with the DirectoryInfo class has two methods viz. GetFiles() and GetDirectories() which return an array of Files and Directories respectively. The GetFiles() method can only be used in the subclass FileInfo. These Files and Directories are then displayed in a ListView by converting the Files and Directories to a ListViewItem. These ListViewItems are created by converting and using the Name Property of the FileInfo and DirectoryInfo subclasses. 



Every File and Directory is contained in a FileInfo and DirectoryInfo class encapsulated with all the properties and functions that help us to identify and use the Files and Directories. The properties include Name, FullName, Creation Time, LastAccessTime, LastWriteTime, Parent, Root to name a few. The functions that apply to this class are the Create, Refresh, Delete, MoveTo etc. These are just some of the functions and properties available to the FileInfo and DirectoryInfo Class. There are many more to explore so make sure you try them all. These are properties that can help identify a particular file or a group of files when you have a large collection. You can filter files from a directory by setting values for these above mentioned properties. We will just use the Name Property to list the Name of the Files and Folders in a Directory for a quick understanding. You can use any other property in much the same way. Watch the code below: 

namespace Show_Files_and_Folders
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("http://www.code-kings.com");
        }

        private void btnChooseDir_Click(object sender, EventArgs e)
        {
            if (DirDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtDirLocation.Text = DirDialog1.SelectedPath.ToString();
            }
        }

        private void btnShowFiles_Click(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(txtDirLocation.Text);
            foreach (System.IO.FileInfo f in dir.GetFiles("*.*"))
            {
                ListViewItem lSingleItem = listView1.Items.Add(f.Name);
            }
        }

        private void btnShowFolders_Click(object sender, EventArgs e)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(txtDirLocation.Text);
            foreach (System.IO.DirectoryInfo getdirs in dir.GetDirectories("*.*"))
            {
                ListViewItem lSingleItem = listView2.Items.Add(getdirs..Name);
            }
        }
    }
}

Directly Download the Source Code in Visual Studio 2012 Here 
Custom Search