How To Delete A File In ASP.NET?
To delete a file on server you can use code like this:
[ C# ]
// We
need this namespace
using
System.IO;
public
partial class
_Default : System.Web.UI.Page
{
protected void
Page_Load(object sender,
EventArgs e)
{
string FileToDelete;
// Set full path to file
FileToDelete =
Server.MapPath("~/Files/SomeFile.txt");
// Delete a file
File.Delete(FileToDelete);
}
}
[ VB.NET ]
' We
need this namespace
Imports
System.IO
Partial
Class _Default
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender
As Object, ByVal
e As System.EventArgs)
Handles Me.Load
Dim
FileToDelete As String
'
Set full path to file
FileToDelete =
Server.MapPath("~/Files/SomeFile.txt")
'
Delete a file
File.Delete(FileToDelete)
End Sub
...
End
Class
Related articles:
1. Introduction To Assemblies