This tutorial will show you how to delete a file on the disk using ASP.NET 2.0 and C#.NET

The .NET Framework offers a number of types that makes accessing resources on filesystems easy to use.
To delete a simple file on the disk, we will need to first import the System.IO namespace. The System.IO namespace contains the File.Delete() method and
FileInfo type that we will use to perform our delete with.

We’ll put our code in the btnSubmit_Click() event.

When the btnSubmit_Click() event fires it first checks to see if the file exists using the FileInfo type. If it exists it runs the File.Delete() method to delete it, otherwise it throws a FileNotFoundException which is caught by one of the catch statements below the try block.

We have one textbox, a Submit button, a label, and a checkbox on the front end for user interaction. The front end .aspx page looks something like this:

The flow for the code behind page is as follows.

Download Source Files