TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Directory.CreateDirectory

This C# example program uses the Directory.CreateDirectory method. The article covers exceptions caused by Directory.CreateDirectory.

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.

Example. 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.

C# program that creates 2 directories

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

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 calls here will succeed.

Overload

And: You can open the C:\ folder on your computer to check the results. The directories should now exist.

Path syntax. The first call uses the double-backslash syntax. A single backslash is the standard escape character in C-style string literals. The second two calls use verbatim string literals, which are prefixed with the @ token.

String Literal

Exceptions. 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.

Tip: Depending on the purpose of your program, you can log these exceptions of terminate your program with an error message.

Exception Handling

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.

Recursive File List

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf