ARTICLE

Create a Directory in C#

Posted by Mahesh Chand Articles | Files, Directory, IO July 16, 2012
How to create a directory or folder using C# and .NET.
Reader Level:

C# Directory Class

The System.IO.Directory class in the .NET Framework class library provides static methods for creating, copying, moving, and deleting directories and subdirectories. Before you can use the Directory class, you must import the System.IO namespace. 

using System.IO;

Create a Directory 

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\Mahesh";
// If directory does not exist, create it. 
if (!Directory.Exists(root))
{
    Directory.CreateDirectory(root);
}

The 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);
}

Download free book: Working with Directories in C#

Login to add your contents and source code to this article
post comment
     

very nice article...useful. Thanks..

Posted by Ajith K Thomas Apr 26, 2013

A directory is a folder on a computer.

Posted by Mahesh Chand Jul 28, 2012

what is directory Ser !!!

Posted by Grabsi Amine Jul 27, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter