Create Nested Directories in C# and VB.NET

A lot of developers do not know that Directory.CreateDirectory() can be used to create directories and subdirectories as specified by the path. Here’s an example

C#

static void Main(string[] args)
{
try
{
Directory.CreateDirectory(@"D:\ParentDir\ChildDir\SubChildDir\");
Console.WriteLine("Directories Created");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

VB.NET

Shared Sub Main(ByVal args() As String)
Try
Directory.CreateDirectory("D:\ParentDir\ChildDir\SubChildDir\")
Console.WriteLine("Directories Created")
Console.ReadLine()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub

OUTPUT

CreateNestedDirectories






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

2 comments:

Unknown said...

The .NET framework comes bundled with a lot of advantages, which makes it widely used tool for .NET application development. In .NET ,
I need some explanation regarding c# to vb conversion..
Anykind of help would be greatly appreciated.

ASP .NET Developers Blog said...

Your article is very interesting. It is very useful and helpful for the Developers to develop the application. Thanks for share this valuable post.