Delete a file in C#

CsharpProgrammingServer Side Programming

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
raja
Published on 11-Sep-2018 12:54:50
Advertisements