C# Program to Delete an Empty and a Non-Empty Directory - GeeksforGeeks

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.
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) methodusing 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) methodusing System;using System.IO;  class GFG{      static void Main(){          // Delete empty directory    // Using Delete() method    Directory.Delete("D:/vignan");    Console.WriteLine("Deleted");}}
Output:
Deleted
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) methodusing 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) methodusing 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
CSharp-File-Handling
Picked
C#
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 C# Interview Questions & Answers
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
C# | Inheritance
Convert String to Character Array in C#
Getting a Month Name Using Month Number in C#
Socket Programming in C#
Program to Print a New Line in C#
Program to find absolute value of a given number