How to Delete a Folder in C#? [3 Methods with Complete Code]

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.

How to Delete a Folder in C#

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: