Hi Friends, in this post I would like to explain how to delete the file existing on the system.
For this I took 1 FileUpload, 1 Button & 1 label control on to the page.
Please find the source code below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="300px">
<tr>
<td>
<asp:FileUpload ID="fuLoad" runat="server" />
</td>
<td>
<asp:Button ID="btnDelete" runat="server" Text="Delete File"
onclick="btnDelete_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblDisplayMessage" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Then please find the Code for deleting the file:
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string filePath = fuLoad.PostedFile.FileName;
if (File.Exists(filePath.Trim()))
{
File.Delete(filePath.Trim());
lblDisplayMessage.Text="File Deleted Successfully";
}
else
{
lblDisplayMessage.Text = "File does not exist.";
}
}
}
Thank You...
Works Good!
Thanks :)