How to use C# Directory Class

The Directory class in C# provides a range of methods for performing essential operations on directories and subdirectories, such as creation, deletion, and relocation. Notably, due to the static nature of the Directory class, there is no need to instantiate an instance of the class. Instead, the methods within the Directory class can be directly invoked from the class itself, streamlining the code execution process. This convenient feature allows for straightforward utilization of the Directory class methods, enhancing the efficiency and readability of C# code related to directory management tasks.

Create a directory using Directory class in C#

To create a new directory in C# using the Directory class, you can directly invoke the CreateDirectory method from within the class itself. This method provides a convenient and straightforward way to generate a new directory within your code. By calling the CreateDirectory method, you can ensure the seamless creation of directories without the need for unnecessary complexities or additional steps. Using the capabilities of the Directory class, this approach allows for efficient and reliable directory creation in C# programming.

Syntax
Directory.CreateDirectory(string DirPath)
  1. DirPath : The name of the new directory
Directory.CreateDirectory("c:\\testDir1");

Check a directory exist or not using Directory class in C#

Before creating a directory or folder, it is common practice to first verify its existence. In C#, this can be accomplished by utilizing the Exists method provided by the Directory class. By invoking the Exists method, you can effectively determine whether the specified directory or folder already exists in the designated location. This allows for proactive handling of scenarios where the directory may already exist, preventing potential conflicts or errors during the creation process. By incorporating the Exists method into your code, you can ensure the robustness and reliability of your directory creation routines in C#.

Syntax
bool Directory.Exist(string DirPath)
  1. DirPath : The name of the directory
  2. bool : Returns true or false
Directory.Exists("c:\\testDir1")

Move a Directory using Directory class in C#

When the requirement arises to relocate a directory, along with its entire contents, from one location to another in C#, the Move method within the Directory class proves to be an invaluable resource. By invoking this method, you can seamlessly transfer the specified directory and all its associated files and subdirectories to a new destination.

Syntax
void Directory.Move(string sourceDirName,string destDirName)
  1. sourceDirName : The source directory we want to move.
  2. destDirName : The destinations directory name.
Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir");

Delete a Directory using Directory class in C#

When the need arises to delete a directory in C#, the Delete method provided by the Directory class serves as a reliable solution. By invoking this method, you can effectively remove the specified directory from the file system.

The Delete method within the Directory class ensures the seamless deletion of the directory and all its contents, including files and subdirectories. This helps maintain a clean and organized file system by removing unnecessary or unwanted directories.

Syntax
void Directory.Delete(string DirPath)
  1. DirPath : The Directory we want to delete.
Directory.Delete("c:\\testDir1");

The following C# source code shows some operations in Directory class

Full Source C#
using System; using System.Windows.Forms; using System.IO; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (Directory.Exists("c:\\testDir1")) { //shows message if testdir1 exist MessageBox.Show ("Directory 'testDir' Exist "); } else { //create the directory testDir1 Directory.CreateDirectory("c:\\testDir1"); MessageBox.Show("testDir1 created ! "); //create the directory testDir2 Directory.CreateDirectory("c:\\testDir1\\testDir2"); MessageBox.Show("testDir2 created ! "); //move the directory testDir2 as testDir in c:\ Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir"); MessageBox.Show("testDir2 moved "); //delete the directory testDir1 Directory.Delete("c:\\testDir1"); MessageBox.Show("testDir1 deleted "); } } } }