Home > Article > Backend Development > C# program to delete empty and non-empty directories
On a computer, we can store files in directories, also called folders. Directories also contain shortcuts to other directories and files. Here, we will discuss C# program to delete empty and non-empty directories. A static class named Directory provides static methods for working with directories. DirectoryInfo objects provide detailed information about a specific directory.
Now that we have a directory (empty or not) we must delete it. An empty directory means that no files or subdirectories exist in the directory. A directory can be thought of as a grouping of files and subdirectories; it may or may not contain data. A directory that contains files or other directories is called a non-empty directory. Using the Delete() method of the Directory class, we can delete a directory. This method has two overloading methods -
Delete(String)
Delete(String,Boolean)
This method is included in the Catalog class. This technique is used to delete empty directories. This method deletes a directory from a defined address or location.
public static void Delete (string DirLocation);
Where DirLocation is the address of the directory that has been provided and we want to delete, the type of this parameter is a string. Now, some errors may occur after executing this command.
When the file with the same name and address specified by DirLocation already exists, an IO exception is thrown. Alternatively, the subfolder is read-only. Unauthorized access exceptions are another type of error that can occur. This exception is thrown if the caller does not have the required permissions. Argument Null Exception This error is thrown when DirLocation is null. Path Too Long Exception is another error that can occur when the given DirLocation, filename, or both exceed the system-defined maximum length. If the DirLocation is missing or cannot be found, a "Directory Not Found" exception occurs. Or the route indicated is wrong.
The following algorithm will step by step give the process of writing a program to delete a directory. In this algorithm, we will use the Delete(String) method.
For example, we will consider an empty directory named "csk". Now, we will use the Delete(String) method and delete the "csk" directory.
Step 1 - We will use the Directory.delete method to delete the directory by passing the directory address.
Step 2 − The deletion of the directory we wrote by using Console.Writeline() has been completed.
// A C# program which goes on given address and deletes the empty directory // Using Delete(string) method using System; using System.IO; class TutPoint { static void Main(){ // Deleting the empty directory by using the Delete() method Directory.Delete("D:/csk"); Console.WriteLine("Deleted"); } }
Deleted
This method is also included in the Directory class. Using this technique will delete the specified directory and any subdirectories and files within that directory, if specified.
public static void Delete (string DirLocation, bool recursive);
This technique is used to delete the specified directory and any subdirectories and files contained within it. Now, some errors may occur after executing this command.
When the file with the same name and address specified by DirLocation already exists, an IO exception is thrown. Alternatively, the subfolder is read-only. Unauthorized access exceptions are another type of error that can occur. This exception is thrown if the caller does not have the required permissions. Argument Null Exception This error is thrown when DirLocation is null.
The path too long exception is another error that occurs when the given DirLocation, filename, or both exceed the maximum length set in the system. If the DirLocation is missing or cannot be found, a "Directory Not Found" exception occurs. Or the route indicated is wrong.
The following algorithm will step by step give the process of writing a program to delete a directory. In this algorithm, we will use Delete(String, Boolean) method.
For example, we will consider that there is a non-empty directory named "csk" in the D drive, which contains a file named "msd". Now, we will use the Delete(String, Boolean) method and delete the "csk" directory.
Step 1 −We will use the Directory.delete(String, Boolean) method to delete a directory by passing the directory address.
Step 2 − Here true is the boolean value passed when checking if the subdirectory exists.
Step 3 − The deletion of the directory we wrote by using Console.Writeline() has been completed.
// A C# program which goes on given address and deletes the non empty directory // Using Delete(string) method using System; using System.IO; class TutPoint { static void Main() { // Deleting the non-empty directory by using the Delete() method Directory.Delete("D:/csk",true); Console.WriteLine("Deleted"); } }
Deleted
In the algorithm we use a single function of the Directory class. Here, the time complexity of Delete(String) will be O(1) and the time complexity of Delete(String, Boolean) will also be O(1).
We have extensively discussed C# programs to delete empty and non-empty directories. First, we discussed the definition of directories and their uses. We then discussed deleting directories in two different ways. Finally, we saw the algorithm and example code. We hope this article helped you enhance your knowledge about C#.
The above is the detailed content of C# program to delete empty and non-empty directories. For more information, please follow other related articles on the PHP Chinese website!