Sunday, 12 June 2011

Deleting files from the system in ASP.Net.

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

<html xmlns="">
<head runat="server">
<title>Untitled Page</title>

<form id="form1" runat="server">
<table width="300px">
<asp:FileUpload ID="fuLoad" runat="server" />
<asp:Button ID="btnDelete" runat="server" Text="Delete File"
onclick="btnDelete_Click" />
<td colspan="2">
<asp:Label ID="lblDisplayMessage" runat="server" Text=""></asp:Label>

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()))
lblDisplayMessage.Text="File Deleted Successfully";
lblDisplayMessage.Text = "File does not exist.";


Thank You...
Shout it

One Response so far.

  1. Anonymous says:

    Works Good!
    Thanks :)

Leave a Reply