create a directory in csharp

To create a directory in C#, you can use the Directory.CreateDirectory method. This method takes a string parameter that represents the path and name of the directory to be created. Here is an example:

main.cs
string path = @"C:\MyNewDirectory";
Directory.CreateDirectory(path);
69 chars
3 lines

This code creates a new directory named "MyNewDirectory" at the root of the C: drive.

You can also use the DirectoryInfo class to create a directory. Here is an example:

main.cs
string path = @"C:\MyNewDirectory";
DirectoryInfo di = Directory.CreateDirectory(path);
88 chars
3 lines

This code creates a new directory named "MyNewDirectory" at the root of the C: drive and stores the directory info in a DirectoryInfo object.

It is important to note that if the directory already exists, the CreateDirectory method will not do anything and will not throw an exception.

gistlibby LogSnag