Do you want to know how to delete a folder in C#? In this tutorial, I will explain how to delete a folder in C# using various methods. I will also show you how to delete folders and subfolders in C# using different methods.
Delete a Folder in C#
I will show you three methods to delete a folder in C# with complete code.
1. Delete a Folder in C# Using Directory.Delete() method
The simplest way to delete a folder in C# is by using the Directory.Delete() method from the System.IO namespace.
Here’s an example of deleting a folder in C#.
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\exampleFolder";
try
{
Directory.Delete(folderPath);
Console.WriteLine("Folder deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
You can see the below code I ran using Visual Studio, and it deleted the folder successfully. Check the screenshot below.
While performing file or folder deletion, various exceptions might occur. Some common exceptions include DirectoryNotFoundException, UnauthorizedAccessException, and IOException.
So, it is always good to write an exception-handling mechanism inside the code. Here is the complete code.
using System;
using System.IO;
class Program
{
static void Main()
{
string folderPath = @"C:\exampleFolder";
try
{
Directory.Delete(folderPath);
Console.WriteLine("Folder deleted successfully.");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine($"Directory not found: {e.Message}");
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine($"Access denied: {e.Message}");
}
catch (IOException e)
{
Console.WriteLine($"IO Error: {e.Message}");
}
}
}
2. Delete a Folder in C# Using DirectoryInfo Class
An alternative to Directory.Delete() is the DirectoryInfo class in C#, which provides more functionalities and control.
Here is a complete example of how to delete a folder in C#.
using System;
using System.IO;
class Program
{
static void Main()
{
DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\exampleFolder");
try
{
directoryInfo.Delete();
Console.WriteLine("Folder deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
3. Delete a Folder in C# Using the Command Line Interface (CLI)
We can also use C# to invoke Command Line commands to delete a folder. Here is a complete example.
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
try
{
Process.Start("cmd.exe", "/c rmdir /s /q C:\\exampleFolder");
Console.WriteLine("Folder deleted successfully.");
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.Message}");
}
}
}
Conclusion
In this tutorial, I have explained how to delete a folder in C# using Directory.Delete() method, using DirectoryInfo Class and the Command Line Interface (CLI).
You may also like:
- How to Delete a File from a Folder in C#
- C# Folder Browser Dialog with TextBox
- Folder Browser Dialog in C#
- C# Folder Naming Conventions
- How to Delete a Folder with Subfolders and Files 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…