File.Delete(String) is an inbuilt File class method which is used to delete the specified file.
Syntax:
public static void Delete (string path);
Parameter: This function accepts a parameter which is illustrated below:
- path: This is the specified file path which is to be deleted.
Exceptions:
- ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters as defined by InvalidPathChars.
- ArgumentNullException: The path is null.
- DirectoryNotFoundException: The given path is invalid.
- IOException: The given file is in use. OR there is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files.
- NotSupportedException: The path is in an invalid format.
- PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
- UnauthorizedAccessException: The caller does not have the required permission. OR the file is an executable file that is in use. OR the path is a directory. OR the path specified a read-only file.
Below are the programs to illustrate the File.Delete(String) method.
Program 1: Before running the below code, a file file.txt is created with some contents shown below:

CSharp
using System;
using System.IO;
public class GFG {
public static void Main()
{
String myfile = @"file.txt";
File.Delete(myfile);
Console.WriteLine("Specified file has been deleted");
}
}
|
Executing:
mcs -out:main.exe main.cs
mono main.exe
Specified file has been deleted
After running the above code, the above output is shown and the file file.txt has been deleted.
Program 2: Before running the below code, two files have been created shown below:


CSharp
using System;
using System.IO;
public class GFG {
public static void Main()
{
String myfile1 = @"file.txt";
String myfile2 = @"gfg.txt";
File.Delete(myfile1);
File.Delete(myfile2);
Console.WriteLine("Specified files have been deleted.");
}
}
|
Executing:
mcs -out:main.exe main.cs
mono main.exe
Specified files have been deleted.
After running the above code, above output is shown and two existing files file.txt and gfg.txt have been deleted.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
12 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...