The System.IO.Directory class offers methods for creating, deleting, moving, retrieving file lists, and many more. These methods are static so you don’t need to create an instance of that class. The following table shows you some useful methods of the Directory class that you can use.

Methods Description
CreateDirectory Creates a new directory.
Delete Deletes an existing directory.
Exists Tells whether a given directory exists in the file system.
GetCurrentDirectory Gets the current working directory of the application.
GetDirectories Gets the name of the subdirectories of the specified directory.
GetFiles Returns the names of the files contained in the specified directory.
GetSystemFileEntries Returns an array of strings which are the names of the files and subdirectories in the specified directory.
GetParent Get the root directory of a specified path.
Move Moves the directory including its contents into another location.

Figure 1

Creating a New Directory


We use the CreateDirectory method to create a new directory. The following code snippet shows how to do that.

Directory.CreateDirectory(@"C:\Directory\Subdirectory");

The CreateDirectory method accepts one string argument which is the path. The CreateDirectory method will create all the directories and subdirectories specified by the path.

Deleting a Directory


Use the Delete method if you want to delete a directory. The Delete method accepts a string argument which is the path of the directory.

Directory.Delete(@"C:\Directory\Subdirectory");

By default, the Delete method will produce an exception if the directory to be deleted is not empty. If you want to include all the files and subdirectory of the directory that you will be deleting, then you can use an overloaded version of the Delete method that has a second boolean parameter. When this is set to true, then everything that the specified directory contains will be deleted as well.

Directory.Delete(@"C:\Directory\Subdirectory", true);

Testing if the Directory Exists


Like the System.IO.File class, the Directory class also has an Exists method which tells whether a specified path of the directory exists inside the file system.

if (Directory.Exists(@"C:\Directory\Subdirectory"))
 Console.WriteLine("The directory exists.");

Getting the List of Subdirectories and Files


The GetDirectories method gets all the subdirectories of the specified path. For demonstration, let’s create a directory C:\Directoryand inside it, create directories named Subdirectory1, Subdirectory2, and Subdirectory3. The following code will list the directories inside C:\Directory.

string[] directories = Directory.GetDirectories(@"C:\");

foreach (string directory in directories)
{
    Console.WriteLine(directory);
}
C:\Directory\Subdirectory1
C:\Directory\Subdirectory2
C:\Directory\Subdirectory3

If you only want to display the subdirectory name, then we do some string manipulation.

Console.WriteLine(directory.Substring(directory.LastIndexOf(@"\") + 1));

Alternatively, you can use the methods by System.IO.Path which will be discussed in a later lesson.

The GetFiles and GetSystemFileEntries are similar to the GetDirectory class. The GetFiles method returns a string array containing the list of files inside the directory. The GetSystemFileEntries is the combined list of files and directories.

Moving a Directory


To move a directory to another location, simply use the Move method which accepts two arguments, the directory to move and the path that the directory will be moved to.

Directory.Move(@"C:\Directory", @"C:\AnotherDirectory\Directory");

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *