ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

CreateDirectory

Recursively create directory based on the given path. If the given path doesn't exist, it will create until all the folders in the path are satisfied.

Source

using System.IO;

public static class MyExtensions
{
	/// <summary>
	/// Recursively create directory
	/// </summary>
	/// <param name="dirInfo">Folder path to create.</param>
	public static void CreateDirectory(this DirectoryInfo dirInfo)
	{
		if (dirInfo.Parent != null) CreateDirectory(dirInfo.Parent);
		if (!dirInfo.Exists) dirInfo.Create();
	}
}

Example

string path = @"C:\temp\one\two\three";

var dir = new DirectoryInfo(path);
dir.CreateDirectory();

Author: Earljon Hidalgo

Submitted on: 26 mrt. 2008

Language: C#

Type: System.IO

Views: 9272