C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Overloads: There are two overloads in the CreateDirectory method group, but this example only shows the one with one parameter.
OverloadAnd: You can open the C:\ folder on your computer to check the results. The directories should now exist.
Syntax: The first call uses the double-backslash syntax. A single backslash is the standard escape character in C-style string literals.
Finally: Two calls use verbatim string literals, which are prefixed with the @ token.
String LiteralC# 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 syntax.
//
Directory.CreateDirectory(@"C:\newfolder2");
//
// Create an already-existing directory (does nothing).
//
Directory.CreateDirectory(@"C:\newfolder2");
}
}
Output
There are 2 folders on your C:\ drive:
1. newfolder
2. newfolder2
Tip: Depending on the purpose of your program, you can log these exceptions of terminate your program with an error message.
ExceptionFurther: We sometimes need to recursively walk through directories. A variety of methods can be used.
Recursive File List