Delete Files From Folder in ASP.NET

Introduction

This article explains how to delete a file, of files listed in a DropDownList, existing in a folder.

Add the following namespaces in the .cs file:

  1. using System;  
  2. using System.Drawing;  
  3. using System.IO;
I have files in my application folder; the folder is named "files". I want to delete some file from those so first I list all the file names in a dropdown list. Write the following code in the .aspx page:

  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head id="Head1" runat="server">  
  3.     <title>how to delete a file from a folder in ASP.Net using C#</title>  
  4.     <style type="text/css">  
  5.         .auto-style1 {  
  6.             width: 88px;  
  7.         }  
  8.     </style>  
  9. </head>  
  10. <body>  
  11.     <form id="form1" runat="server">  
  12.         <div>  
  13.             <table>  
  14.                 <tr>  
  15.                     <td>Select file to delete</td>  
  16.                     <td class="auto-style1">  
  17.                         <asp:DropDownList ID="DropDownList1" runat="server" Height="16px" Width="88px">  
  18.                             <asp:ListItem>Select file</asp:ListItem>  
  19.                             <asp:ListItem>file1.txt</asp:ListItem>  
  20.                             <asp:ListItem>file2.pdf</asp:ListItem>  
  21.                             <asp:ListItem>file3.doc</asp:ListItem>  
  22.                             <asp:ListItem></asp:ListItem>  
  23.                         </asp:DropDownList>  
  24.                     </td>  
  25.                 </tr>  
  26.                 <tr>  
  27.                     <td></td>  
  28.                     <td class="auto-style1">  
  29.                         <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />  
  30.                     </td>  
  31.                 </tr>  
  32.                 <tr>  
  33.                     <td colspan="2">  
  34.                         <asp:Label ID="lbl_output" runat="server" /></td>  
  35.                 </tr>  
  36.             </table>  
  37.         </div>  
  38.     </form>  
  39. </body>  
  40. </html>
Code.cs

When the dropdown has all file names you can select any file to be deleted and click on the button "Delete". It will check if the file exists; if it exists then it will delete file else show you a message in the lbl_output that your file does not exist.

Write the following code on the button delete:
  1. protected void btnDelete_Click(object sender, EventArgs e)  
  2. {  
  3.       string file_name = DropDownList1.SelectedItem.Text;  
  4.       string path = Server.MapPath("files//" + file_name);  
  5.       FileInfo file = new FileInfo(path);  
  6.       if (file.Exists)//check file exsit or not  
  7.       {  
  8.            file.Delete();  
  9.            lbl_output.Text = file_name + " file deleted successfully";  
  10.            lbl_output.ForeColor = Color.Green;  
  11.       }  
  12.       else  
  13.       {  
  14.            lbl_output.Text = file_name + " This file does not exists ";  
  15.            lbl_output.ForeColor = Color.Red;  
  16.       }  
  17. }
Save all and view the page in the browser; it will work fine.