C# Create Directory

C# Create Directory

You can create directory using your own program written in .net languages.
The namespace System.Io provides such features. Following code snippets explains various scenarios. Create

Create Directory at application execution path.


using System;
using System.IO;
namespace ConsoleApplication
{    
class Program    
{        
static void Main(string[] args)        
{            
string currentExecutablepath = Directory.GetCurrentDirectory();            
string directoryName = "MySetup";            
string path = currentExecutablepath + @"\" + directoryName;            
Directory.CreateDirectory(path);            
Console.ReadKey();        
}         
}
}

   The above program created directory called MySetup at below path.

D:\demo\ConsoleApplication\ConsoleApplication\bin\Debug

Create Directory at root drive C:\

using System;
using System.IO;
namespace ConsoleApplication
{    
class Program    
{        
static void Main(string[] args)        
{            
Directory.CreateDirectory(@"c:\MyDirectory");            
Console.ReadKey();        
}         
}
}

c# create directory if it doesn't exist

using System;
using System.IO;
namespace ConsoleApplication
{    
class Program    
{        
static void Main(string[] args)        
{            
if( ! Directory.Exists(@"c:\MyDirectory"))            
{                            
Directory.CreateDirectory(@"c:\MyDirectory");            
}            
Console.WriteLine("Directory already exists.");            
Console.ReadKey();        
}         
}
}

Description: 

Directory.GetCurrentDirectory();  gives current directory execution path.

For more information about c# get current directory of executable

Directory.Exists(@"c:\MyDirectory"); Returns boolean value of whether directory exists at or not.

Summery:
  CreateDirectory is static method of class Directory. System.IO is the required namespace for
  File I/O operations.

Note: While dealing with File I/O operations there are possibilities of getting exception based on environment rights like create directory rights etc.
Based on those exceptions you can create log file or alert users about those exception and user can take appropriate actions.

Tags:

Asp.net Create Folder On ServerC# Delete All Files In DirectoryC# Get Current Directory of Executable(c#) write to text fileC#-move files from foldersC# foreach loopC# XML XmlReaderc# stringc# arraylistC# DateTime DifferenceCreate contact us form

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.