C# Program to Delete an Empty and a Non-Empty Directory
Last Updated :
30 Nov, 2021
Given a directory(empty or non-empty), now we have to delete the given directory. Here, an empty directory means the directory is present without any files or subdirectories. We can define a directory as a collection of files and subdirectories, a directory may have data or not contain no data. The non-empty directory means the directory with files or subdirectories. We can delete the directory by using the Delete() method of the Directory class. This method is overloaded in two different ways:
- Delete(String)
- Delete(String, Boolean)
Let's discuss them one by one.
Delete(String)
This method is used to delete an empty directory from a given path or location.
Syntax:
public static void Delete (string Mypath);
Where Mypath is the location of the given directory that we want to remove and the type of this parameter is a string.
Exceptions:
It can have the following exceptions
- IOException: This exception occurs when a file with the same name and location given by Mypath exists. Or the directory is read-only.
- UnauthorizedAccessException: This exception will occur when the caller does not have the specified permission.
- ArgumentNullException: This exception will occur when the Mypath is null.
- PathTooLongException: This exception will occur when the given Mypath, file name, or both exceed the system-defined maximum length.
- DirectoryNotFoundException: This exception will occur when the Mypath does not exist or could not be found. Or the given path is invalid.
Example 1:
Let us consider an empty directory named "sravan" in the D drive. Now using Delete(String) method we delete the "sravan" directory.
C#
// C# program to delete the empty directory
// Using Delete(string) method
using System;
using System.IO;
class GFG{
static void Main()
{
// Delete empty directory
// Using Delete() method
Directory.Delete("D:/sravan");
Console.WriteLine("Deleted");
}
}
Output:
Deleted
Example 2:
Let us consider a non-empty directory named "vignan" with a file named "test" in the D drive. Now using Delete(String) method we will delete the "vignan" directory.
C#
// C# program to delete the empty directory
// Using Delete(string) method
using System;
using System.IO;
class GFG{
static void Main()
{
// Delete empty directory
// Using Delete() method
Directory.Delete("D:/vignan");
Console.WriteLine("Deleted");
}
}
Output:
Deleted
Delete(String, Boolean)
This method is used to delete the given directory and if indicated, any subdirectories and files in the directory.
Syntax:
public static void Delete (string Mypath, bool recursive);
Where Mypath is the directory path and recursive is used to remove files, directories, etc if it is true. Otherwise false.
Exceptions:
It can have the following exceptions
- IOException: This exception occurs when a file with the same name and location specified by Mypath exists. Or the directory is read-only.
- UnauthorizedAccessException: This exception will occur when the caller does not have the required permission.
- ArgumentNullException: This exception will occur when the Mypath is null.
- PathTooLongException: This exception will occur when the specified Mypath, file name, or both exceed the system-defined maximum length.
- DirectoryNotFoundException: This exception will occur when the Mypath does not exist or could not be found.
Example 1:
Let us consider an empty directory named "vignan" in the D drive. Now using Delete(String, Boolean) method we will delete the "vignan" directory.
C#
// C# program to delete the empty directory
// Using Delete(String, Boolean) method
using System;
using System.IO;
class GFG{
static void Main()
{
// Delete empty directory
// Using Delete(String, Boolean) method
Directory.Delete("D:/vignan", true);
Console.WriteLine("Deleted");
}
}
Output:
Deleted
Example 2:
Let us consider a non-empty directory named "sravan" with a file named "test" in the D drive. Now using Delete(String, Boolean) method we will delete the "sravan" directory.
C#
// C# program to delete the non-empty directory
// Using Delete(String, Boolean) method
using System;
using System.IO;
class GFG{
static void Main()
{
// Delete non-empty directory
// Using Delete(String, Boolean) method
Directory.Delete("D:/sravan", true);
Console.WriteLine("Deleted");
}
}
Output:
Deleted
Similar Reads
C# Program to Create a Directory A directory is a file system that stores file. Now our task is to create a directory in C#. We can create a directory by using the CreateDirectory() method of the Directory class. This method is used to create directories and subdirectories in a specified path. If the specified directory exists or t
2 min read
C# Program to Check Given Directory Exists or not Given a directory, now our task is to check given directory exists or not. So to this task, we use the Exists() method of the Directory class. This method will return true if the given directory exists, otherwise false. Syntax: public static bool Exists (string? Mypath); Where, Mypath is a parameter
2 min read
C# Program to Get Root Directory of Given Directory Directory class provides different types of methods for creating, moving, deleting, renaming, and modifying directories and subdirectories. GetDirectoryRoot() is a method of Directory class. This method is used to find the information of the volume or root or both for the given path. Or we can say t
2 min read
C# Program to Demonstrate the Use of CreateSubdirectory Method DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc. This class has a CreateSubdirectory() method that is used to create a sub-directory or sub-directories on the given path. Here the
2 min read
C# Program to Show the Use of Exists Property DirectoryInfo class provides different types of methods and properties for creating, moving, deleting, renaming, and modifying directories and subdirectories. Exists property is the property of DirectoryInfo class. This property is used to check whether a directory exists or not and return the boole
1 min read