Do you want to delete all files in a directory with an extension in C#? Check out this complete tutorial; I have explained how to delete all files in a directory with a specific extension in C#.
I will show you several methods, such as using DirectoryInfo, Directory, and LINQ.
Delete All Files in a Directory With a Specific Extension in C#
Below are the 3 methods you can use
- Using DirectoryInfo Class
- Using Directory Class
- Using LINQ
Delete All Files in a Directory With Extension in C# using DirectoryInfo Class
The DirectoryInfo class provides instance methods for creating, moving, and browsing through directories and subdirectories. Below is a complete C# code snippet that demonstrates how to delete all .txt files in a given directory using DirectoryInfo.
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
DirectoryInfo di = new DirectoryInfo(directoryPath);
foreach (FileInfo file in di.GetFiles())
{
if (file.Extension == ".txt")
{
file.Delete();
}
}
}
}
Here is the code explanation:
- Import the System.IO namespace to use the DirectoryInfo class.
- Define the path of the directory where the files are located.
- Create an instance of DirectoryInfo and pass the directory path to it.
- Iterate through all the files in the directory.
- Use an if statement to check the extension of each file.
- Delete the file if its extension is .txt.
You can see the below screenshot. I have a folder having a few files with different extensions like “.txt”, “.docx” and “.xlsx”.
Now, when I run the console application, you can see it deletes both the .txt files, but it does not delete the other extension files; check the screenshot below.
Delete All Files in a Directory With Extension in C# using Directory Class
The Directory class provides static methods for creating, moving, and enumerating through directories and subdirectories in C#. Below is the code snippet using Directory to delete all .txt files in a directory in C#.
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
string[] files = Directory.GetFiles(directoryPath);
foreach (string file in files)
{
if (Path.GetExtension(file) == ".txt")
{
File.Delete(file);
}
}
}
}
Here is the complete code explanation.
- Import the System.IO namespace.
- Define the path of the directory.
- Use Directory.GetFiles() to get all files in the directory.
- Iterate through the files and check their extensions.
- Delete the file if its extension is .txt.
Delete All Files in a Directory With Extension in C# using LINQ
If you are comfortable with LINQ (Language-Integrated Query), you can use the below code to delete all .txt files in a directory in C#.
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
string directoryPath = @"C:\ExampleDirectory";
var txtFiles = Directory.GetFiles(directoryPath).Where(file => Path.GetExtension(file) == ".txt");
foreach (string file in txtFiles)
{
File.Delete(file);
}
}
}
Here is the complete code explanation:
- Import the System.IO and System.Linq namespaces.
- Define the directory path.
- Use LINQ to filter files with .txt extension.
- Delete the files.
Conclusion
We’ve explored three different methods to delete all files with a specific extension in a directory using C#:
- Using the DirectoryInfo class for instance-level operations.
- Using the Directory class for static methods.
- Using LINQ for a more concise codebase.
You may also like the following tutorials:
- How to Delete a Folder with Subfolders and Files in C#?
- How to Delete All Files in a Folder Older Than 7 Days in C#?
- How to Delete a Folder in C#?
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…