asp.net - How to delete a directory

Delete a directory programmatically

Directory.Delete(String) Method
.Net framework Directory.Delete(String) method allow us to delete an empty directory from a specified path. this method require to pass a parameter named 'path'. this 'path' represents the name of the empty directory to remove. this directory must be writable and empty.

Delete(String) method throw IOException, AunauthorizedAccessException, ArgumentException, ArgumentNullException, PathTooLongException and DirectoryNotFoundException exception.

Directory.Delete(String, Boolean) Overloaded method.
.Net framework Directory.Delete(String, Boolean) overloaded method allow us to delete the specified directory and if indicated, any sub directories and files in the directory. this method require to pass two parameters named 'path' and 'recursive'. the 'path' parameter represents the name of the directory to remove.

'recursive' parameter value data type is System.Boolean. this parameter value 'true' indicate that it will remove directories, sub directories and files in path. 'false' value indicate method only remove empty directory.

Delete(String, Boolean) method throw IOException, UnauthorizedAccessException, ArgumentException, ArgumentNullException, PathTooLongException and DirectoryNotFoundException.

.Net framework DirectoryInfo class DirectoryInfo.Delete() method also allow us to delete the DirectoryInfo (specified directory) if it is empty. in the bellow example code we uses this DirectoryInfo.Delete() method to delete an empty directory.

the following asp.net c# example code demonstrate us how can we delete an empty directory programmatically at run time in an asp.net application.
DirectoryDelete.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack)
        {
            Label1.Text = "Physical Application Path: " + Request.PhysicalApplicationPath;
            Label1.Text += "<br/ >Delete a directory from here.";
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string appPath = Request.PhysicalApplicationPath.ToString();
                
        DirectoryInfo deleteDirectory = new DirectoryInfo(appPath+TextBox1.Text);
        try
        {
            deleteDirectory.Delete();
            Label2.Text = "Directory deleted successfully!";
        }
        catch (Exception ex)
        {
            Label2.Text = "an error occured when deleting!";
        }            
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to delete a directory programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net example: delete directory</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="SeaGreen"
            >
        </asp:Label>
        <br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="Crimson"
            Font-Italic="true"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label3" 
            runat="server" 
            Text="Directory Name"
            >
        </asp:Label>
        <asp:TextBox ID="TextBox1" runat="server">
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="HotPink"
            Text="Delete Directory"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples