Create And Delete Directory or Folder in ASP.NET Using C#

Introduction

In this article, I will explain how to create and delete a directory or folder from your drive in ASP.NET using C#. When you are running a community website where the user posts articles, forum messages, etc. When a user registers in a website a folder can be created at run time, then when you upload or ask a query, the data can be automatically placed inside the user's own directory instead of the root or anywhere else. You must import the following namespace:

using System.IO;

Complete Program

Create_and_Remove_Directory.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;     // Add This Namespace  
  8. public partial class Create_and_Remove_Directory : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.     }  
  13.     protected void RadButton1_Click(object sender, EventArgs e)  
  14.     {  
  15.         string path = @"D:\" + txtname.Text;  // Give the specific path  
  16.         if (!(Directory.Exists(path)))  
  17.         {  
  18.             Directory.CreateDirectory(path);  
  19.             Sucesslbl.Text = "Directory Created Successfully";  
  20.         }  
  21.         else  
  22.         {  
  23.             Sucesslbl.Text = "Already Directory Exits With Same Name";  
  24.         }  
  25.     }  
  26.     protected void RadButton2_Click(object sender, EventArgs e)  
  27.     {  
  28.         string path = @"D:\" + deletetxt.Text;  
  29.         if (Directory.Exists(path))  
  30.         {  
  31.             DeleteDirectory(path);  
  32.         }  
  33.         else  
  34.         {  
  35.             Deletelbl.Text = "Directory not exits";  
  36.         }  
  37.     }  
  38.     private void DeleteDirectory(string path)  
  39.     {  
  40.         // Delete all files from the Directory  
  41.         foreach (string filename in Directory.GetFiles(path))  
  42.         {  
  43.             File.Delete(filename);  
  44.         }  
  45.         // Check all child Directories and delete files  
  46.         foreach (string subfolder in Directory.GetDirectories(path))  
  47.         {  
  48.             DeleteDirectory(subfolder);  
  49.         }  
  50.         Directory.Delete(path);  
  51.         Deletelbl.Text = "Directory deleted successfully";  
  52.     }  
  53. }  
Create_and_Remove_Directory.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Create_and_Remove_Directory.aspx.cs" Inherits="Create_and_Remove_Directory" %>  
  2. <%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>  
  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></title>  
  7.     <style type="text/css">  
  8.         .style1  
  9.         {  
  10.             width: 230px;  
  11.         }  
  12.         .style2  
  13.         {  
  14.             width: 169px;  
  15.         }  
  16.         .style3  
  17.         {  
  18.             width: 230px;  
  19.             height: 26px;  
  20.         }  
  21.         .style4  
  22.         {  
  23.             width: 169px;  
  24.             height: 26px;  
  25.         }  
  26.         .style5  
  27.         {  
  28.             height: 26px;  
  29.         }  
  30.         .style6  
  31.         {  
  32.             width: 230px;  
  33.             height: 29px;  
  34.         }  
  35.         .style7  
  36.         {  
  37.             width: 169px;  
  38.             height: 29px;  
  39.         }  
  40.         .style8  
  41.         {  
  42.             height: 29px;  
  43.         }  
  44.     </style>  
  45. </head>  
  46. <body>  
  47.     <form id="form1" runat="server">  
  48.     <div>  
  49.         <asp:ScriptManager ID="ScriptManager1" runat="server">  
  50.         </asp:ScriptManager>  
  51.         <h3>Create and Remove Directory in ASP.NET</h3>  
  52.         <table style="width: 47%;">  
  53.             <tr>  
  54.                 <td class="style1" style="font-weight: bold">  
  55.                     Enter File Name to Create</td>  
  56.                 <td class="style2">  
  57.                     <asp:TextBox ID="txtname" runat="server"></asp:TextBox>  
  58.                 </td>  
  59.                 <td>  
  60.                      </td>  
  61.             </tr>  
  62.             <tr>  
  63.                 <td class="style3">  
  64.                 </td>  
  65.                 <td class="style4">  
  66.                     <telerik:RadButton ID="RadButton1" runat="server" Font-Bold="True"  
  67.                         onclick="RadButton1_Click" Text="Create Directory">  
  68.                     </telerik:RadButton>  
  69.                 </td>  
  70.                 <td class="style5">  
  71.                     <asp:Label ID="Sucesslbl" runat="server" ForeColor="Red"></asp:Label>  
  72.                 </td>  
  73.             </tr>  
  74.             <tr>  
  75.                 <td class="style6" style="font-weight: bold">  
  76.                     Enter File Name to Delete</td>  
  77.                 <td class="style7">  
  78.                     <asp:TextBox ID="deletetxt" runat="server"></asp:TextBox>  
  79.                 </td>  
  80.                 <td class="style8">  
  81.                 </td>  
  82.             </tr>  
  83.             <tr>  
  84.                 <td class="style1">  
  85.                      </td>  
  86.                 <td class="style2">  
  87.                     <telerik:RadButton ID="RadButton2" runat="server" Font-Bold="True"  
  88.                         onclick="RadButton2_Click" Text="Delete Directory">  
  89.                     </telerik:RadButton>  
  90.                 </td>  
  91.                 <td>  
  92.                     <asp:Label ID="Deletelbl" runat="server" ForeColor="Red"></asp:Label>  
  93.                 </td>  
  94.             </tr>  
  95.         </table>   
  96.     </div>  
  97.     </form>  
  98. </body>  
  99. </html>  

Output 1

 first-image.jpg


Enter the directory name and then click on the "Create Directory" button:

 enter-directory-name.jpg


Output 2

If you will again click on Create Directory button then:

 re-click-on-create-directory.jpg

Output 3

Open the drive and you will see a new directory created, as in:

 create-directory-in-drive.jpg


Output 4

Now enter a name in the "remove" TextBox and then click on the "Delete Directory" button:

 click-on-delete-button.jpg


Output 5

If you will again click on Delete Directory button then:

 re-click-on-delete-button.jpg


Output 6

Now, open the drive, a new directory is removed from your drive, as in:

 remove-directory-from-drive.jpg

For more information, download the attached sample application.


Similar Articles