Delete a file in C#



Use File.Delete method to delete a file.

Firstly, set the path of the file you want to delete.

String myPath = @"C:\New\amit.txt";

Now, use the File.Delete method to delete the file.

File.Delete(myPath);

The following is the complete code −

Example

 Live Demo

using System; using System.IO; public class Program {    public static void Main() {       String myPath = @"C:\New\amit.txt";       Console.WriteLine("Deleting File");       File.Delete(myPath);    } }

Output

Deleting File
Updated on: 2020-06-23T07:12:15+05:30

943 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements