How to Delete Folder In .Net Core Using C#.Net

How to get all Sub folder or directory present in a parent folder in .NET core using C#.net and delete a single or all directory or folder using c#.
In today's article I will show you how you can delete the folder in .NET core using c#.net. We have already checked how we can create a folder in .NET core. So, for this article first we will use console application to perform the example. Here we will learn how we can delete a specific folder and second to delete all the sub folders in one go. So first we will create folder.

Sub folder in windows
After creating the sub folder, we need to create a .NET core console application.  In this program.cs under main method add the below code.

Code to Delete Single Sub Folder in .NET Core With C#.Net

To remove a single sub folder first we need to check that the specific fub folder exists in the parent folder or not. If it exists on that case delete the folder. Please check the below code.

//parent directory path
string parentDirectoryPath = "D:\\DemoRepo\\Myfolder";
string deleteDirectoryName = "2";
if (Directory.Exists(parentDirectoryPath))
{
    string subDirPath = string.Format("{0}\\{1}", parentDirectoryPath, deleteDirectoryName);
    //Get List of all directorys
    List<string> subDirecoryList = new List<string>();
    subDirecoryList = Directory.GetDirectories(parentDirectoryPath).ToList();
    //Validate directory exists 
    bool directoryStatus = (from d in subDirecoryList
                            where d == subDirPath
                            select d).Any();
    if (directoryStatus)
    {
        Directory.Delete(subDirPath, true);
        Console.WriteLine("Directory deleted successfully.");
    }
    else
    {
        Console.WriteLine("Directory not found.");
    }
}
In above code first I have stored the path of the parent directory. Now I have taken a variable where I am defining the directory name which I want to delete, here if you are using in an application, you can dynamically pass the folder name to delete. 

After defining the variables, I have validated whether the parent folder exists or not. If folder exists on that case i have prepared the path for the subdirectory. 
    List<string> subDirecoryList = new List<string>();
    subDirecoryList = Directory.GetDirectories(parentDirectoryPath).ToList();
In above piece of code i have created a List of string type. After that Directory.GetDirectories() method used to read all the sub directory available in parent folder. Now by using Linq to validate that the sub directory exists or not. If it exists i ma using Directory.Delete to delete the sub folder by passing the path of the sub directory.  Now let's run the application by applying a break pint and check the output.

List of Sub Directory or folder Using C#.net

In above you can see we have got the list of all directories available in the parent folder. After that verification of sub directory exists or not done with the help of LINQ. 
    //Validate directory exists 
    bool directoryStatus = (from d in subDirecoryList
                            where d == subDirPath
                            select d).Any();
Here if sub directory exists on that case we will get true otherwise false. On the bases of return value, we are deleting the directory. 
    if (directoryStatus)
    {
        Directory.Delete(subDirPath, true);
        Console.WriteLine("Directory deleted successfully.");
    }
    else
    {
        Console.WriteLine("Directory not found.");
    }
Now check the parent folder. Now we are having only 2 sub directories. 

Delete Folder or Directory In .Net Core Using C#

Code to Delete All Sub Folder in .NET Core with C#.Net

Here we will see how we can check and delete all sub folder available in parent directory using C#.net in .NET core.  
string parentDirectoryPath = "D:\\DemoRepo\\Myfolder";
if (Directory.Exists(parentDirectoryPath))
{
    //Get List of all sub directorys
    var subDirecoryList = Directory.GetDirectories(parentDirectoryPath).ToList();
    foreach (var sub in subDirecoryList)
    {
        if (Directory.Exists(sub))
        {
            Directory.Delete(sub, true);
        }
    }
    Console.WriteLine(string.Format("Directory '{0}' deleted successfully.", +subDirecoryList.Count()));
}
Now let's understand the above piece of code. Here first I have taken the parent directory folder path. In current time parent directory holding two sub directories.  folder 1 and 3. Now to validate the parent directory exists or not I have used Directory.Exists(-). If it exists on that case, it will proceed further.  Please check the below code. 
 var subDirecoryList = Directory.GetDirectories(parentDirectoryPath).ToList();
In above code i am reading all the sub directory or folder available in the parent folder or directory. This will return list of all sub directory available. After getting all the Sub directory or folders we will user foreach to delete each and every sub directory. 
 foreach (var sub in subDirecoryList)
    {
        if (Directory.Exists(sub))
        {
            Directory.Delete(sub, true);
        }
    }
In above code with the foreacch loop I am validating whether the sub directory or the folder exists or not. If it exists on that case by using Directory.Delete to delete the sub folder. After deleting all the directory, a user-friendly message has been displayed. Now let's run the code by putting a break point. 

C# code to delete sub folder

Here in above image we are getting two sub folder or directory. Now press F5 and check the output. We got success message.
How to Delete Folder In .Net Core Using C#

Now check the parent folder.

Empty folder

Post a Comment