Manage Files 3D (Display, Download & Delete) From a Folder in ASP.Net

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

Up Next
    Ebook Download
    View all
    Learn
    View all