Home »
.Net »
C# Programs
C# program to delete a text file
In this C# program, we are going to learn how to delete a text file? Method File.Delete() is using to delete the file.
Submitted by IncludeHelp, on October 29, 2017
Given a file, and we have to delete it using C# program.
File.Delete()
This is a method of "File" class, it deletes the given file.
Syntax:
void File.Delete(path);
Parameter(s):
path - Filename with its location.
Program
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
//method to delete file
File.Delete("123.TXT");
//confirmation message
Console.WriteLine("File deleted successfully");
}
}
}
Output
File deleted successfully
Note: In above program, we need to remember, when we use "File" class, System.IO namespace must be included in the program. An exception is not thrown if the specified file does not exist.
C# File Handling Programs »