Create new folders with the Directory.CreateDirectory method from System.IO.
Directory.CreateDirectory creates a new folder. It allows us to easily create new directories. It is a static method. As with all calls to file or directory-handing methods, it can throw exceptions. These should be handled.
First, the method we use here is called Directory.CreateDirectory and it is available in the System.IO namespace in the base class library. You can either add the using directive at the top of your file, or use the fully qualified name.
Overloads: There are two overloads in the CreateDirectory method group, but this example only shows the one with one parameter.
C# program that creates 2 directories
static void Main()
// Create new folder in C:\ volume.
// Create another directory with different syntax.
// Create an already-existing directory (does nothing).
There are 2 folders on your C:\ drive:
Exceptions. When interfacing with the file system, you should always prepare for exceptions and provide code for recovering from them. The CreateDirectory method throws 7 types of exceptions.
Tip: Depending on the purpose of your program, you can log these exceptions of terminate your program with an error message.
Clear. The CreateDirectory method will not 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.
Further: We sometimes need to recursively walk through directories. A variety of methods can be used.
Summary. We invoked the Directory.CreateDirectory method. We tested a program and discussed issues related to this method. We provided hints on specifying folder paths and on further uses of CreateDirectory.