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 Get Complete Path of Current Directory
Given a directory, now our task is to find the path of the given directory or current directory. So to this task, we use the GetCurrentDirectory() method of the Directory class. This method will return the complete path of the current directory. The result given by this method will not end with a ba
1 min read
C# Program to Get the List of Sub-Directories of a Given Directory
Given a director, now we will find the list of the sub-directories present in the given directory. So to this task, we use the GetDirectories() method of the Directory class. This method is used to get the list of directories/sub-directories from the given directory or sub-directories. We have to sp
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 Search Sub-Directory in a Given Directory
C# is a general-purpose,  object-oriented programming language pronounced as âC Sharpâ. It is a lot syntactically similar to Java and is easy for users who have knowledge of C, C++, or Java. In this article, we will learn How can we use C# to search the sub-Directory in a given Directory. So for  t
3 min read
C# Program to Get the List of Files From Given Directory
Given a directory, now we will find the list of files from the given directory. So for this, we use the GetFiles() method of the Directory class. This method is used to find the list of files from the given directory or sub directories. The overloaded methods of this method are: 1. GetFiles(String):
2 min read
C# Program to Demonstrate the Use of FullName Property
DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc and FullName property is one of them. This property is used to find the full path of the directory or the specified file. Syntax: p
1 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