aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Delete a File from Uploaded Folder using C#, VB.NET

Dec 12, 2012

Here I will explain how to delete a file from a folder in using c# and

In previous articles I explained
Difference between sessionstate and viewstate, Highlight gridview rows on mouseover in jQuery, and many articles relating to, c#, and JQuery. Now I will explain how to delete a file from a folder in using c# and

To implement this functionality we need to write the following code like as shown below

string FileName ="test.txt";
string Path = "E:\\" + FileName;
FileInfo file = new FileInfo(Path);
if (file.Exists)
If you want to see it in complete example check below code

<html xmlns="">
<head runat="server">
<title>how to delete a file from a folder in using c#</title>
<form id="form1" runat="server">
<td><b>Enter File Name to Delete:</b></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<tr><td></td><td><asp:Button ID="btnDelete" runat="server" Text="Delete File" /> </td></tr>
<tr><td colspan="2"><asp:Label ID="lblResult" runat="server"/></td></tr>
Now open code behind file and add following namespaces

C# Code

using System;
using System.Drawing;
using System.IO;
After that write the following code in code behind

protected void btnDelete_Click(object sender, EventArgs e)
string filename =txtName.Text;
string path = "E:\\" + filename;
FileInfo file = new FileInfo(path);
if (file.Exists)
lblResult.Text = filename + " file deleted successfully";
lblResult.ForeColor = Color.Green;
lblResult.Text = filename + " file not exists in filepath";
lblResult.ForeColor = Color.Red;

Imports System.Drawing
Imports System.IO
Partial Class Default2
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim filename As String = txtName.Text
Dim path As String = "E:\" & filename
Dim file As New FileInfo(path)
If file.Exists Then
lblResult.Text = filename & " file deleted successfully"
lblResult.ForeColor = Color.Green
lblResult.Text = filename & " file not exists in filepath"
lblResult.ForeColor = Color.Red
End If
End Sub
End Class

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


tinhdamsugia said...

day la comment

tinhdamsugia said...

public void DeleteFileFromFolder(string fileName)
// Day la thu muc trong website cua ban - This is folder in your website.
string strPhysicalFolder = Server.MapPath("../Images/Article/");
string strFileFullPath = strPhysicalFolder + fileName;
FileInfo file = new FileInfo(strFileFullPath);
if (file.Exists)

tinhdamsugia said...

Các bạn xử dụng phương thức trên, truyền vào filename cần xóa là ok. - You can used to above method, transfer your filename to delete.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.