Healthcare IT Blog

A place to blog about Healthcare Informatics Software Development Implementations

image

Create multilevel directories/folders using C#

By Technical Team Technosoft |14 August 2014

This function will create a directory.

For example: when we pass the path "C:\folder1\folder2\folder3".

folder1 exists and folder2 and folder3 do not exist. This function will create both folder2 and folder3.

 public static void CreateDirectory(string path)
{
if (!Directory.Exists(path) && !path.Equals(string.Empty))
{
string[] paths = path.Split('\\');
string _path = paths[0];
 
for (int i = 1; i < paths.Length; i++)
{
_path = _path + "\\" + paths[i];
 
if (!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}
}
}
}