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

1
Introduction:

Here I will explain how to delete a file from a folder in

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)
{
file.Delete();
}

If you want to see it in complete example check below code

Enter File Name to Delete:

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)
{
file.Delete();
lblResult.Text = filename + " file deleted successfully";
lblResult.ForeColor = Color.Green;
}
else
{
lblResult.Text = filename + " file not exists in filepath";
lblResult.ForeColor = Color.Red;
}

VB.NET Code



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
file.Delete()
lblResult.Text = filename & " file deleted successfully"
lblResult.ForeColor = Color.Green
Else
lblResult.Text = filename & " file not exists in filepath"
lblResult.ForeColor = Color.Red
End If
End Sub
End Class
Here it is Demo Idea
[Image: KSbE5.gif]
#Praise the creator not the creations#
-Mr.lonely
We can do it by VB with out any cons
[Image: An9XX.gif]

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