Asp.net delete file file on Server C#.net and VB.net code
In this post I will show you how to delete particular file on server or file uploaded at upload folder. Demo has both C#.net and VB.net code.
<title>Asp.net File Upload and Delete</title>
<form id="form1" runat="server">
<asp:TextBox ID="txtFileName" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Delete" OnClick="Button2_Click" />
Asp.net C# code behind to delete file on Server
public partial class FileUploadDemo : System.Web.UI.Page
protected void Button2_Click(object sender, EventArgs e)
string filePath = Server.MapPath("uploads");
filePath = filePath + @"\" + txtFileName.Text;
Note: First we check for the existence of the file on server. This will avoid file not found exception to occur.
Asp.net with VB.net code Delete file on Server
Public Partial Class FileUploadDemo
Protected Sub Button2_Click(sender As Object, e As EventArgs)
Dim filePath As String = Server.MapPath("uploads")
filePath = (filePath & "\") + txtFileName.Text
If File.Exists(filePath) Then
We are asking users to input the name of the file on server to delete. I have some files inside upload folder that folder you too need to create if you are running above example. The name of folder is upload .
You can also see how to upload file on server