C# Program to Delete Empty and Nonempty Directory


Introduction

On the computer, we can store files in a directory, also known as a folder. A directory also contains shortcuts to other directories and files. Here, we will discuss the C# Program to Delete Empty and Non-empty Directories. A static class called Directory offers static methods for dealing with directories. A DirectoryInfo object offers details on a particular directory.

Delete Empty and Non-empty Directory

Now that we have a directory (either empty or not), we must delete it. An empty directory indicates that there are no files or subdirectories present 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 referred to as non-empty. Using the Delete() method of the Directory class, we can remove the directory. There are two ways in which this method is overloaded −

  • Delete(String)

  • Delete(String,Boolean)

Delete(String)

This method is contained in the Directory Class. This technique is used to delete an empty directory. This method deletes the directory from the defined address or location.

public static void Delete (string DirLocation); 

Where DirLocation is the address of the directory that has been provided and that we want to delete, and this parameter's type is a string. Now, there can be some errors that can happen after the execution of this command.

When a file with the same name and address specified by DirLocation already exists, an IO Exception is thrown. Alternatively, the subfolder is read-only. An unauthorized Access Exception is another type of error that can happen. If the caller does not possess the required authorization, this exception will be thrown. Argument Null Exception happens when the DirLocation is empty, this error will be thrown. Path Too Long Exception is another error that can happen when the given DirLocation, file name, or both exceed the system-defined maximum length, this exception will arise. The Directory Not Found Exception will appear if the DirLocation is missing or cannot be located. or the indicated route is erroneous.

Algorithm

The algorithm below will give a step by step procedure to write a program which deletes the directory. In this algorithm we will be using Delete(String) method.

For example, we are going to consider an empty directory named “csk”. Now, we are going to use Delete(String) method and will delete the “csk” directory.

Step 1 − We’ll be using the Directory.delete method for deleting the directory by passing the directory address.

Step 2 − By using Console.Writeline() we write that the deletion of the directory is completed.

Example

// 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");
   }
}

Output

Deleted

Delete(String, Boolean)

This method is also contained in the Directory Class. The specified directory, as well as, if specified, any subdirectories and files within the directory, are deleted using this technique.

public static void Delete (string DirLocation, bool recursive); 

This technique is used to remove the specified directory as well as any subdirectories and files contained within it. Now, there can be some errors that can happen after the execution of this command.

When a file with the same name and address specified by DirLocation already exists, an IO Exception is thrown. Alternatively, the subfolder is read-only. An unauthorized Access Exception is another type of error that can happen. If the caller does not possess the required authorization, this exception will be thrown. Argument Null Exception happens when the DirLocation is empty, this error will be thrown.

Path Too Long Exception is another error that can happen when the given DirLocation, file name, or both of them surpass the maximum length that is set in the system then this exception will arise. The Directory Not Found Exception will appear if the DirLocation is missing or cannot be located. or the indicated route is erroneous.

Algorithm

The algorithm below will give a step by step procedure to write a program which deletes the directory. In this algorithm we will be using Delete(String, Boolean) method.

For example, we are going to consider a non empty directory named “csk” with a file named “msd” in the D drive. Now, we are going to use Delete(String, Boolean) method and will delete the “csk” directory.

Step 1  We'll be using the Directory.delete(String, Boolean) method for deleting the directory by passing the directory address.

Step 2  Here true is the boolean being passed as it checks for subdirectories' existence.

Step 3 − By using Console.Writeline() we write that the deletion of the directory is completed.

Example

// 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");
   }
}

Output

Deleted

Time Complexity

In the algorithm we are using a single function of the Directory class. Here, the time complexity for Delete(String) will be O(1) as well as for Delete(String, Boolean) will be O(1) too.

Conclusion

We have extensively discussed the C# Program to Delete Empty and Non-empty Directories. Firstly, we talked about the directory definition and its use. Then we talked about deleting the directory in two different ways. Finally, we saw the algorithm and the example code. We hope that this article has helped you enhance your knowledge regarding C#.

Updated on: 21-Apr-2023

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements