Get Filenames From Folder in Gridview Control in ASP.Net

Introduction

This article introduces the Directory class and shows how to get the file names from a folder into a GridView control.

Directory Class

The System.IO.Directory class in the .NET Framework class library provides static methods for creating, copying, moving, and deleting directories and subdirectories. For using the Directory class you need to use the System.IO namespace.

Directory.GetFiles method

The GetFiles method returns the names of files in a specified directory or returns a string array of file names.

Step 1

Open Visual Studio then seelct "Create New Website" --> "ASP.NET Web Site".

CreteApp

Step 2

Now go to the Solution Explorer to the right side of the application and add a new folder named "MyFiles" that contains the file steps as in the following figure.

NewFolder

Step 3

Now go to the Solution Explorer to the right side of the application and add a new item as in the following figure.

NewItem

Step 4

Now add a new web form to your website. as in the following figure.

NewWebForm

Write the following code in a Default.aspx page:

Step 5

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10.     <asp:Button ID="btnGetFiles" Text="Get Filenames from MyFiles Folder" runat="server" onclick="GetFiles" />  
  11.     <asp:GridView ID="gvFiles" CellPadding="3" runat="server" AutoGenerateColumns="False"  
  12.  BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical">  
  13.     <AlternatingRowStyle BackColor="#DCDCDC" />  
  14.     <Columns>  
  15.        <asp:BoundField DataField="Text" HeaderText="FileName" />  
  16.     </Columns>  
  17.     <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />  
  18.     <HeaderStyle BackColor="#000084" Font-Bold="true" ForeColor="White" />  
  19.     <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />  
  20.     <RowStyle BackColor="#EEEEEE" ForeColor="Black" />  
  21.     <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />  
  22.     <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  23.     <SortedAscendingHeaderStyle BackColor="#0000A9" />  
  24.     <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  25.     <SortedDescendingHeaderStyle BackColor="#000065" />  
  26. </asp:GridView>  
  27. </div>  
  28. </form>  
  29. </body>  
  30. </html>   

Design View of Default.aspx page

DesignView

Now write the following code in Default.aspx.cs.

Step 6

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Configuration;  
  9. using System.IO;  
  10. using System.Web.UI.WebControls;  
  11. public partial class _Default : System.Web.UI.Page  
  12. {  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.     }  
  16.     protected void BindGridview()  
  17.     {  
  18.         string[] filesLoc = Directory.GetFiles(Server.MapPath("~/MyFiles/"));  
  19.         List<ListItem> files = new List<ListItem>();  
  20.         foreach (string file in filesLoc )  
  21.         {  
  22.             files.Add(new ListItem(Path.GetFileName(file)));  
  23.         }  
  24.         gvFiles.DataSource = files;  
  25.         gvFiles.DataBind();  
  26.     }  
  27.     protected void GetFiles(object sender, EventArgs e)  
  28.     {  
  29.         BindGridview();  
  30.     }  
  31. } 

Step 7

Debug the application by pressing F5 to execute the Web form. After debugging the application the output will be as in the following figure:

Debug

Step 8

Now click on the button to get all the file names from the folder "MyFiles" as in the following figure.

GetFilenames

Summary

This article has shown how to use the Directory class and Directory.GetFiles method. For interacting with the file system and using folders the Directory class is to be used in the application.


Similar Articles