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

Advertisements