[C#] How to create a Directory in C#

The Directory.CreateDirectory method creates a directory in the specified path with the specified Windows security. You can also create a directory on a remote compute. The following code snippet creates a Temp folder in C:\ drive if the directory does not exists already.

string root = @"C:\Temp";
string subdir = @"C:\Temp\csharpcanban.com";
// If directory does not exist, create it.
if (!Directory.Exists(root))
{
    Directory.CreateDirectory(root);
}

The Directory.CreateDirectory is also used to create a sub directory. All you have to do is to specify the path of the directory in which this subdirectory will be created in. The following code snippet creates a Mahesh subdirectory in C:\Temp directory.

// Create sub directory
if (!Directory.Exists(subdir))
{
    Directory.CreateDirectory(subdir);
}

 

 

Xem thêm  [C#] How to Get and Set File Last Write Time

Leave a Reply

Your email address will not be published. Required fields are marked *