In Focus

List All Files in a Folder Based on File Extension in ASP.NET

This is a very simple article on how to search and list all the files in a folder based on the file extension provided.

This is a very simple article on how to search and list all the files in a folder based on the file extension provided. I am using an ASP.NET website example developed using Visual Studio 2008. You can use any version of .NET to work on this example.

I have placed a few files in the location C:\Test for this example.

Image 1.jpg

Now create a web site and add a text box control, listbox and a submit button. The textbox takes the file extension to be searched and by default, if no extension is specified ".txt" is taken and searched against the folder.

My ASPX file content looks as in the following:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    Enter Extension of the file to be searched:<asp:TextBox ID="FileExtension" runat="server"></asp:TextBox>

    <br />

    Files List:<br />

    <asp:ListBox ID="FilesList" runat="server"></asp:ListBox>

    <br />

    <br />

    <asp:Button ID="Submit" Text="Search" runat="server"/>

    </div>

    </form>

</body>

</html>

My code-behind file looks as in the following:

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{

    /// <summary>

    /// Page Load event code

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void Page_Load(object sender, EventArgs e)

    {

        //Bind the file names to listbox control

        BindFileNamesToList();

 

        //Event Handler for search button click

        Submit.Click += new EventHandler(Submit_Click);

    }

 

    /// <summary>

    /// Submit button click functionality

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    void Submit_Click(object sender, EventArgs e)

    {

        //Bind the file names to listbox control

        BindFileNamesToList();

    }

 

    /// <summary>

    /// Get the files list by searching the files

    /// in the folder with the extension provided.

    /// </summary>

    private void BindFileNamesToList()

    {

        //get the file extension to be searched from the textbox

        string extension = FileExtension.Text;

 

        //Get the file information from the folder C:\Test based on the extension to be searched.

        //FileInfo class has methods for copying, moving, renaming, creating, opening, deleting,

        //and appending to files.

        FileInfo[] fileInfo = GetFilesFromFolder(@"C:\Test", (extension == "") ? "txt" : extension);

 

        //Refer http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx for more details.

 

        //Clear the listbox items before loading.

        FilesList.Items.Clear();

 

        //Loop thru the fileInfo array object to get each fileInfo object and get the name of the file.

        //Append the filename as a value and text of a listbox

        foreach (FileInfo fileInfoTemp in fileInfo)

        {

            ListItem listItem = new ListItem(fileInfoTemp.Name, fileInfoTemp.Name);

            FilesList.Items.Add(listItem);

        }

    }

 

    /// <summary>

    /// Method to get the files list

    /// </summary>

    /// <param name="folderName"></param>

    /// <param name="extension"></param>

    /// <returns></returns>

    FileInfo[] GetFilesFromFolder(string folderName, string extension)

    {

        DirectoryInfo directoryInfo = new DirectoryInfo(folderName);

 

        string internalExtension = string.Concat("*.", extension);

 

        FileInfo[] fileInfo = directoryInfo.GetFiles(internalExtension, SearchOption.AllDirectories);

 

        return fileInfo;

    }

}

Now let us run the web site and see how it works.


Image 2.jpg

Since I did not specify any file extension, it takes ".txt" as default. Now let us enter the extension and search.

Image 3.jpg

Search for other types.

Image 4.jpg

Hope you liked this article. You can add more search functionalities by allowing a user to enter other criteria and search the files using the FileInfo class.