How to delete file from folder.???

Ranjeet_8
Posted by Ranjeet_8 under ASP.NET category on | Points: 40 | Views : 1957
when the user click on button then if the file exists then it will be deleted.
// HTML

<%@ 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Delete" onclick="Button1_Click" />
</div>
</form>
</body>
</html>


// C# Code

using System.IO;
protected void Button1_Click(object sender, EventArgs e)
{
FileInfo TheFile = new FileInfo(MapPath(".") + "\\Your Folder Location\\" + TextBox1.Text + ".Doc");
if (TheFile.Exists)
{ File.Delete(MapPath(".") + "\\Your Folder Location\\" + TextBox1.Text + ".Doc");
}
}

Comments or Responses

Login to post response