How to delete file from folder.???

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Delete" onclick="Button1_Click" />

// 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