BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Basic Folder Operations

Some applications that perform file access also require the ability to manipulate the folder structure, or directory structure, within the file system. The Directory class provides many static methods, including some that perform basic folder operations.

Folder Operations

When you are developing an application that interacts with the file system you may wish to modify the folder structure on a disk or other device. You might want to create new directories, move folders between locations, rename folders or delete them completely. All of these operations are available through the .NET framework's Directory class. In this article we'll look at the static methods of this class that allow these actions.

The Directory class is found in the System.IO namespace, so add the following using directive to simply the code:

using System.IO;

Creating a Folder

If you follow the examples in this article you'll create a folder with several subfolders, before moving and renaming some folders and finally deleting the parent folder. The parent folder is named "test" and is stored in the root of the C: drive. If you already have such a folder, change the paths used in the examples.

To create a new folder you can use the Directory class' CreateDirectory method. This is a static method, like all of the methods of the class, so does not require an object to be instantiated. The basic overloaded version of the method requires one parameter. This accepts a string containing the name of the folder to create. You can provide an absolute path containing a drive letter, use a UNC path or provide a relative path, which will be created beneath the current working folder. If the folder already exists, the method performs no action.

To create a folder named, "test" in the root of the C: drive, execute the following:

Directory.CreateDirectory(@"c:\test");

If you need to create a deeply nested directory structure you do not need to call the CreateDirectory method multiple times. One call containing a long path is sufficient; all of the folders in the path will be created if they do not exist.

For example, the following code creates the "subfolder", "subfolder 2" and "subfolder 3" directories.

Directory.CreateDirectory(@"c:\test\subfolder\subfolder 2\subfolder 3");

Moving a Folder

If you wish to relocate a folder you can do so using the Move method. This method requires two parameters. The first holds the path of the folder that you wish to move. The second is the path that the folder will have after the relocation.

For example, the following call moves the "subfolder 2" directory up one level into the "test" folder.

Directory.Move(@"c:\test\subfolder\subfolder 2", @"c:\test\subfolder 2");

Renaming a Folder

The Move method can also be used to rename a folder. Again, the source folder's path is specified using the first argument and the new path, including the new name, is provided to the second parameter.

The code below renames "subfolder" to "subfolder 1".

Directory.Move(@"c:\test\subfolder", @"c:\test\subfolder 1");

Deleting a Folder

To remove a folder from a disk completely you can use the Delete method. Two overloaded versions are available, depending upon the action that you wish to take. If you only wish to remove a folder if it is empty, you can use the version with one parameter, which accepts the path of the directory to be deleted.

If you try to delete the "test" folder in this manner you will see an exception, as the folder is not empty.

Directory.Delete(@"c:\test");

If you wish to delete folders when they are not empty you can add a second, Boolean argument. If the argument is set to false, only empty folders will be erased. If set to true, the deletion is recursive, removing the folder and all of its contents.

To clean up the folder structure and leave it as we found it, delete the "test" folder and its contents by running the following code:

Directory.Delete(@"c:\test", true);
5 March 2012