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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

john alphen 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.