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. 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:

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.     Enter Extension of the file to be searched:<asp:TextBox ID="FileExtension" runat="server"></asp:TextBox>  
  11.     <br />  
  12.     Files List:<br />  
  13.     <asp:ListBox ID="FilesList" runat="server"></asp:ListBox>  
  14.     <br />  
  15.     <br />  
  16.     <asp:Button ID="Submit" Text="Search" runat="server"/>  
  17.     </div>  
  18.     </form>  
  19. </body>  
  20. </html> 

My code-behind file looks as in the following:

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.HtmlControls;  
  9. using System.Web.UI.WebControls;  
  10. using System.Web.UI.WebControls.WebParts;  
  11. using System.Xml.Linq;  
  12. using System.IO;  
  13. public partial class _Default : System.Web.UI.Page  
  14. {  
  15.     /// <summary>  
  16.     /// Page Load event code  
  17.     /// </summary>  
  18.     /// <param name="sender"></param>  
  19.     /// <param name="e"></param>  
  20.     protected void Page_Load(object sender, EventArgs e)  
  21.     {  
  22.         //Bind the file names to listbox control  
  23.         BindFileNamesToList();  
  24.         //Event Handler for search button click  
  25.         Submit.Click += new EventHandler(Submit_Click);  
  26.     }  
  27.     /// <summary>  
  28.     /// Submit button click functionality  
  29.     /// </summary>  
  30.     /// <param name="sender"></param>  
  31.     /// <param name="e"></param>  
  32.     void Submit_Click(object sender, EventArgs e)  
  33.     {  
  34.         //Bind the file names to listbox control  
  35.         BindFileNamesToList();  
  36.     }  
  37.     /// <summary>  
  38.     /// Get the files list by searching the files  
  39.     /// in the folder with the extension provided.  
  40.     /// </summary>  
  41.     private void BindFileNamesToList()  
  42.     {  
  43.         //get the file extension to be searched from the textbox  
  44.         string extension = FileExtension.Text;  
  45.         //Get the file information from the folder C:\Test based on the extension to be searched.  
  46.         //FileInfo class has methods for copying, moving, renaming, creating, opening, deleting,  
  47.         //and appending to files.  
  48.         FileInfo[] fileInfo = GetFilesFromFolder(@"C:\Test", (extension == "") ? "txt" : extension);  
  49.         //Refer http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx for more details.  
  50.         //Clear the listbox items before loading.  
  51.         FilesList.Items.Clear();  
  52.         //Loop thru the fileInfo array object to get each fileInfo object and get the name of the file.  
  53.         //Append the filename as a value and text of a listbox  
  54.         foreach (FileInfo fileInfoTemp in fileInfo)  
  55.         {  
  56.             ListItem listItem = new ListItem(fileInfoTemp.Name, fileInfoTemp.Name);  
  57.             FilesList.Items.Add(listItem);  
  58.         }  
  59.     }  
  60.     /// <summary>  
  61.     /// Method to get the files list  
  62.     /// </summary>  
  63.     /// <param name="folderName"></param>  
  64.     /// <param name="extension"></param>  
  65.     /// <returns></returns>  
  66.     FileInfo[] GetFilesFromFolder(string folderName, string extension)  
  67.     {  
  68.         DirectoryInfo directoryInfo = new DirectoryInfo(folderName);  
  69.         string internalExtension = string.Concat("*.", extension);  
  70.         FileInfo[] fileInfo = directoryInfo.GetFiles(internalExtension, SearchOption.AllDirectories);  
  71.         return fileInfo;  
  72.     }  
  73. }   

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.


Similar Articles