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.
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
- {
-
-
-
-
-
- protected void Page_Load(object sender, EventArgs e)
- {
-
- BindFileNamesToList();
-
- Submit.Click += new EventHandler(Submit_Click);
- }
-
-
-
-
-
- void Submit_Click(object sender, EventArgs e)
- {
-
- BindFileNamesToList();
- }
-
-
-
-
- private void BindFileNamesToList()
- {
-
- string extension = FileExtension.Text;
-
-
-
- FileInfo[] fileInfo = GetFilesFromFolder(@"C:\Test", (extension == "") ? "txt" : extension);
-
-
- FilesList.Items.Clear();
-
-
- foreach (FileInfo fileInfoTemp in fileInfo)
- {
- ListItem listItem = new ListItem(fileInfoTemp.Name, fileInfoTemp.Name);
- FilesList.Items.Add(listItem);
- }
- }
-
-
-
-
-
-
- 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.
Since I did not specify any file extension, it takes ".txt" as default. Now let us enter the extension and search.
Search for other types.
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.