title: How to delete the file inside data folder?
body: <p>I done the code where when I upload the file, it will be saved into folder call Data and can be download too (I didn't use database at all). When the file can be upload, and download, I can't make the delete button works. Is there any way to make this happen? Thank you. </p>
  
  <p>here is my design code :</p>
  
  <pre><code>        &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;
      &lt;asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;br /&gt;
      &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting"&gt;
          &lt;Columns&gt;
              &lt;asp:TemplateField HeaderText="File"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='&lt;%# Eval("File") %&gt;' CommandName="Download" Text='&lt;%# Eval("File") %&gt;'&gt;&lt;/asp:LinkButton&gt;
                      &lt;br /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:BoundField DataField="Size" HeaderText="Size In Bytes" /&gt;
              &lt;asp:BoundField DataField="Type" HeaderText="File Type" /&gt;
              &lt;asp:TemplateField HeaderText="Delete"&gt;
              &lt;ItemTemplate&gt;
              &lt;span onclick="return confirm('Are you sure to Delete the record?')"&gt;
              &lt;asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"&gt;&lt;/asp:LinkButton&gt;
              &lt;/span&gt;
              &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
          &lt;/Columns&gt;
          &lt;FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /&gt;
          &lt;HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /&gt;
          &lt;PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /&gt;
          &lt;RowStyle BackColor="White" ForeColor="#330099" /&gt;
          &lt;SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /&gt;
          &lt;SortedAscendingCellStyle BackColor="#FEFCEB" /&gt;
          &lt;SortedAscendingHeaderStyle BackColor="#AF0101" /&gt;
          &lt;SortedDescendingCellStyle BackColor="#F6F0C0" /&gt;
          &lt;SortedDescendingHeaderStyle BackColor="#7E0000" /&gt;
      &lt;/asp:GridView&gt;
      &lt;br /&gt;
  </code></pre>
  
  <p>here is my code behind : </p>
  
  <pre><code>public partial class ScheduleUpload : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          if(FileUpload1.HasFile)
          {
              FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Data/") + FileUpload1.FileName);
          }//end if FileUpload1
  
          DataTable dt = new DataTable();
          dt.Columns.Add("File", typeof(string));
          dt.Columns.Add("Size", typeof(string));
          dt.Columns.Add("Type", typeof(string));
  
  
          foreach (string strFile in Directory.GetFiles(Server.MapPath("~/Data/")))
          {
              FileInfo fi = new FileInfo(strFile);
  
              dt.Rows.Add(fi.Name, fi.Length, GetFileTypeByExtension(fi.Extension));
  
          }//end foreach
  
          GridView1.DataSource = dt;
          GridView1.DataBind();
      }///end btn1_Click
  
      private string GetFileTypeByExtension(string extension)
      {
          switch(extension.ToLower())
          {
              case ".doc":
              case ".docx":
                  return "Microsoft Word Document";
  
              case ".xlsx":
              case ".xls":
                  return "Microsoft Excel Document";
  
              case ".txt":
                  return "Text Document";
  
              case ".jpg":
              case ".png":
                  return "Image";
  
              default:
                  return "Unknown";
  
          }//end switch
      }//end GetFileTypeByExtension
  
      protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if(e.CommandName == "Download")
          {
              Response.Clear();
              Response.ContentType = "application/octect-stream";
              Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
              Response.TransmitFile(Server.MapPath("~/Data/") + e.CommandArgument);
              Response.End();
          }//end if
      }//end GridView_RowCommand
  
      protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          File.Delete(MapPath(("~/File/")));
  
      }
  }
  </code></pre>