13 Mar 2012

Application to Show the List of All Files in a Folder-Developed in C#

This Application is used to list all the files in a folder or directory. This application is developed by .NET Framework and the C# programming language. The framework provides powerful methods on the Directory class that can help you list files on the file system. The Directory.GetFiles method allows you to get a string array of the file names.
Below code sample returns the List Of All Files in All Directories In Given Path.
string[] Files= Directory.GetFiles(folderPath,"*.*",SearchOption.AllDirectories);
The Below image is the UI of this Application.
First we have select the path for this application to show the list of files present in that path.This application is developed by using C# language and .NET framework Environment.
Code for this Application:
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 DirectoryInfo
{
    public partial class Form1 : Form
    {
        string folderPath;
        Options OpForm = new Options();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            txtFileInfo.ReadOnly = true;
            btnOptions.Enabled = false;
            btnOk.Enabled = false;
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            btnBrowse.Enabled = false;
            folderBrowserDialog1.ShowDialog();
            folderPath = folderBrowserDialog1.SelectedPath.ToString();
            txtPath.Text = folderPath;
            btnOk.Enabled = true;
            btnOptions.Enabled = true;
        }
        private void btnOptions_Click(object sender, EventArgs e)
        {
            txtFileInfo.Text = "";
            OpForm.Activate();
            OpForm.ShowDialog();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            getFileList();
        }
      
        public void getFileList()
        {
            btnOk.Enabled = false;
            label1.Text = "Searching....";
            label1.Refresh();
            string[] allFiles;
            txtFileInfo.Text = "Files In " + folderPath.ToUpper() + Environment.NewLine;
            try
            {
                if (OpForm.SubDirect == "true")
                {
                    //Get the List of Files with specified Extention in All Directories.
                    allFiles = Directory.GetFiles(folderPath, OpForm.FileName + "*" + OpForm.FileType, SearchOption.AllDirectories);
                }
                else if (OpForm.SubDirect == "false")
                {
                    //Get the List of Files with specified Extention in Top Directories Only
                    allFiles = Directory.GetFiles(folderPath, OpForm.FileName + "*" + OpForm.FileType, SearchOption.TopDirectoryOnly);

                }
                else
                {
                    //Get the List Of All Files in All Directories In the Path Selected.
                    allFiles = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);

                }

                if (allFiles.Length == 0)
                    txtFileInfo.Text = "No File Found With this Search...";
                else
                {
                    for (int i = 0; i < allFiles.Length; i++)
                    {
                        FileInfo fi = new FileInfo(allFiles[i].ToString());
                        //Write the Files Names in a Multiline Text Box.
                        txtFileInfo.Text += Environment.NewLine + (i + 1).ToString() + ")" + fi.Name.ToString();
                        txtFileInfo.Select(txtFileInfo.Text.Length, 0);
                        txtFileInfo.ScrollToCaret();
                        txtFileInfo.Refresh();

                    }
                }
            }
            catch (Exception ea)
            {
                MessageBox.Show("Error: " + ea.Message);
            }
            label1.Text = "Search Compleated.";
            label1.Refresh();

            btnOk.Enabled = true;
            btnBrowse.Enabled = true;
         }

       
    }
}
This application having the another form to give the options to the search method.
The below image is the UI of Child form:
This Application Search the files includes in Sub directories and specific extension files too.
The Source Code for this Child form :

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;
namespace DirectoryInfo
{
    public partial class Options : Form
    {

        string selItem;
        string fName;
        string subDir;
        public Options()
        {
            InitializeComponent();
        }
        public string FileType
        {
            set
            {
                selItem = value;
            }
            get
            {
                return selItem;
            }
        }
        public string FileName
        {
            set
            {
                fName = value;
            }
            get
            {
                return fName;
            }
        }
        public string SubDirect
        {
            set
            {
                subDir = value;
            }
            get
            {
                return subDir;
            }
        }

        private void Options_Load(object sender, EventArgs e)
        {
           
            checkBox1.Checked = true;
            subDir = "true";
            label3.Hide();
            textBox2.Hide();
            comboBox1.Items.Clear();
            comboBox1.Items.Add("All Files");
            comboBox1.Items.Add(".txt");
            comboBox1.Items.Add(".xml");
            comboBox1.Items.Add(".jpeg");
            comboBox1.Items.Add(".png");
            comboBox1.Items.Add("Other");
            comboBox1.SelectedIndex = 0;

        }
        private void btnOkOptn_Click(object sender, EventArgs e)
        {
            if (selItem == "Other")
                selItem = textBox2.Text;

            fName = textBox1.Text;
           
            this.Close();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            selItem=comboBox1.SelectedItem.ToString();
            if (selItem == "All Files")
                selItem = ".*";
            else if (selItem == "Other")
            {
                label3.Show();
                textBox2.Show();
            }
            else
            {
                label3.Hide();
                textBox2.Hide();
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
                subDir = "true";
            else
                subDir = "false";
        }
    }
}

For Downloading the entire solution file CLICK HERE
For Downloading the Set Up file for this application CLICK HERE