This article shows how to show files from a folder in a Grid View in ASP.NET C#. I am also showing how to download and delete a file from a folder.

The following is my aspx code:

  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>Manage Files (Show, Download & Delete)</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <table cellpadding="10" cellspacing="10" style="border: solid 10px red; background-color: Skyblue;"
  10. width="90%" align="center">
  11. <tr>
  12. <td style="height: 35px; background-color: Yellow; font-weight: bold; font-size: 16pt;
  13. font-family: Times New Roman; color: Red" align="center">
  14. Manage Files (Show, Download & Delete) of A Folder
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>
  19. <asp:GridView ID="GridViewShowFiles" runat="server" AutoGenerateColumns="False" EmptyDataText="No files uploaded"
  20. CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" HeaderStyle-HorizontalAlign="Left">
  21. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  22. <Columns>
  23. <asp:BoundField DataField="Text" HeaderText="File Name" />
  24. <asp:TemplateField>
  25. <ItemTemplate>
  26. <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("Value") %>'
  27. runat="server" OnClick="DownloadFile"></asp:LinkButton>
  28. </ItemTemplate>
  29. </asp:TemplateField>
  30. <asp:TemplateField>
  31. <ItemTemplate>
  32. <asp:LinkButton ID="lnkDelete" Text="Delete" CommandArgument='<%# Eval("Value") %>'
  33. runat="server" OnClick="DeleteFile" />
  34. </ItemTemplate>
  35. </asp:TemplateField>
  36. </Columns>
  37. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  38. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  39. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  40. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  41. <EditRowStyle BackColor="#999999" />
  42. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  43. </asp:GridView>
  44. </td>
  45. </tr>
  46. </table>
  47. </form>
  48. </body>
  49. </html>
My aspx.cs code:
  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. using System.Collections.Generic;
  14. public partial class _Default : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (!IsPostBack)
  19. {
  20. GettAllFiles();
  21. }
  22. }
  23. protected void GettAllFiles()
  24. {
  25. if (System.IO.Directory.Exists(Server.MapPath("~/MyDirectory/")))
  26. {
  27. string[] filePaths = Directory.GetFiles(Server.MapPath("~/MyDirectory/"));
  28. List<ListItem> files = new List<ListItem>();
  29. foreach (string filePath in filePaths)
  30. {
  31. files.Add(new ListItem(Path.GetFileName(filePath), filePath));
  32. }
  33. GridViewShowFiles.DataSource = files;
  34. GridViewShowFiles.DataBind();
  35. }
  36. else
  37. {
  38. GridViewShowFiles.DataSource = null;
  39. GridViewShowFiles.DataBind();
  40. }
  41. }
  42. protected void DownloadFile(object sender, EventArgs e)
  43. {
  44. string filePath = (sender as LinkButton).CommandArgument;
  45. Response.ContentType = ContentType;
  46. Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
  47. Response.WriteFile(filePath);
  48. Response.End();
  49. }
  50. protected void DeleteFile(object sender, EventArgs e)
  51. {
  52. string filePath = (sender as LinkButton).CommandArgument;
  53. File.Delete(filePath);
  54. Response.Redirect(Request.Url.AbsoluteUri);
  55. }
  56. }
Now run the application:

manage file
Image 1

Now download a file:

manage file of a folder
Image 2

You can delete any file by clicking the Delete button:

show download and delete document
Image 3