C# Directory.CreateDirectory

Directory illustration

You need to create a new folder or directory in your program's execution, using the C# programming language. Fortunately, the .NET Framework has an excellent method in the System.IO namespace that allows you to easily create new directories. We look at the method called Directory.CreateDirectory, which is a static method that allows you create new directories and folders.

Example

First, the method we require here is called Directory.CreateDirectory and it is available in the System.IO namespace in the base class library from the .NET Framework. You can either add the using directive at the top of your file, or use the fully qualified name "System.IO.Directory.CreateDirectory."

This C# example program uses the Directory.CreateDirectory method.

Program that creates 2 directories [C#]

using System.IO;

class Program
{
    static void Main()
    {
	//
	// Create new folder in C:\ volume.
	//
	Directory.CreateDirectory("C:\\newfolder");
	//
	// Create another directory with different string literal syntax.
	//
	Directory.CreateDirectory(@"C:\newfolder2");
	//
	// Create an already-existing directory (does nothing).
	//
	Directory.CreateDirectory(@"C:\newfolder2");
    }
}

Result of program
    There are 2 folders on your C:\ drive:
    1. newfolder
    2. newfolder2

Using Directory.CreateDirectory method. There are two overloads in the CreateDirectory method group, but this example only shows the one with one parameter. The other overload allows you to specify security options. The three Directory.CreateDirectory calls here will succeed and you can check the C:\ folder on your computer to check the results.

Path syntax. The first method invocation to CreateDirectory uses the double-backslash syntax. This is required because a single backslash is the standard escape character in C-style string literals. The second two CreateDirectory calls use verbatim string literals, which are prefixed with the @ token.

Errors and exceptions

Warning

When interfacing with the file system, you should always prepare for exceptions and provide code for recovering from them. The CreateDirectory method throws seven types of exceptions, as shown on the MSDN reference page. Depending on the purpose of your program, you can simply log these exceptions of terminate your program with an error message or dialog box.

Clear folders

Programming tip

The CreateDirectory method shown in this article will not immediately erase all the contents of the folder. To clear the entire directory if it exists, you can call Directory.Exists, and then Directory.GetFiles, and finally File.Delete on each path. Additionally, you sometimes need to recursively walk through directories.

Recursive File List

Summary

The C# programming language

We saw how you can invoke the Directory.CreateDirectory method in the C# programming language. Additionally, we tested the program and discussed issues related to the Directory.CreateDirectory static public method. The article gives you some hints about specifying folder paths and information on some limitations of the method.

File Handling
.NET