How to delete a file programmatically in asp.net c#

Delete a file programmatically
.Net framework File.Delete() method allow us to delete the specified file. File class Delete() method exists in System.IO namespace. Delete() method has a required parameter named 'path'.

this 'path' parameter value data type is System.String. this string value represents the name of the file to be deleted. in this parameter value wildcard characters are not supported.

Delete() method throw ArgumentException exception, if 'path' is a zero length string, contains only white space or contains one or more invalid characters. it throw ArgumentNullException, if 'path' is null. this method throw NotSuportedException, if 'path' is an invalid format.

Delete() method throw UnauthorizedAccessException, if the caller does not have the required permission or 'path' is a directory or 'path' specified a read-only file. this method also throw DirectoryNotFoundException, IOException and PathTooLongException.

if the file to be deleted does not exists, no exception is thrown. we need to specify a file name with any relative or absolute path information for the 'path' parameter.

the following asp.net c# example code demonstrate us how can we delete a file from web server file system programmatically at run time in an asp.net application.
FileDelete.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack)
        {
            TextBox1.Text += Request.PhysicalApplicationPath + "Elephant.jpg";
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string filePath = Request.PhysicalApplicationPath + "Elephant.jpg";

        try 
        {
            File.Delete(filePath);
            Label1.Text = "File deleted";

        }
        catch(Exception ex)
        {
            Label1.Text = "an error occured<br />";
            Label1.Text = ex.Message.ToString();
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to delete a file programmatically in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Fuchsia">asp.net example: file delete</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="DarkGreen"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Text="File"
            ForeColor="DarkSlateBlue"
            Font-Bold="true"
            >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server"
             BackColor="DarkSlateBlue"
             ForeColor="Snow"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="DarkSlateBlue"
            Text="Delete File"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples