How to Delete a File in C#/CSharp

To delete a file in C#/CSharp, we make use of File class and its static method called Delete.  It will take path of the file as parameter.  Example is given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
using System.IO;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Users\ADMIN\Desktop\delete.txt";
File.Delete(path);
Console.WriteLine("File Deleted!");
}
}
}
using System; using System.IO; namespace Hello_World { class Program { static void Main(string[] args) { string path = @"C:\Users\ADMIN\Desktop\delete.txt"; File.Delete(path); Console.WriteLine("File Deleted!"); } } }
using System;
using System.IO;

namespace Hello_World
{

    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\ADMIN\Desktop\delete.txt";

            File.Delete(path);

            Console.WriteLine("File Deleted!");
        }
    }
}