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
C++ Program to Get the List of Files in a Directory Getting the list of files in a directory is one of the most common operations performed by the Filesystem of an OS. The file explorer application in most operating systems performs the same operation in the background. In this article, you will learn how to get the list of files in a directory using
4 min read
How to Check a File or Directory Exists in C++? Checking the presence of a directory or a file is one of the most common operations performed by a file system in an Operating System. Most programming languages offer some level of file system accessibility in form of library functions. In this article, you will learn how to test a file or director
4 min read
Different Ways to Empty or Delete a Large File Content in Linux In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file. Before we proceed with the given various ways, please make sure that the file we're emptying or deleting content is
3 min read
R - Check if a Directory Exists and Create if It does not Directories and sub-directories are accessed by their corresponding paths in the R Programming Language. It is easy to work with these in R and perform operations related to the creation, copy, and movement of folders and sub-folders within the system. In this article, we will see how to check if a
2 min read
How to recursively delete a directory and its entire contents (files + sub dirs) in PHP? In PHP if you want to delete the file or directory then keep one thing in mind that you cannot delete the file or directory directly there is a condition on that i.e. there are some security issues are there so the best way to do this is you first have to delete the data present in the file or the s
4 min read