Evgeniy Labunskiy
Evgeniy Labunskiy

Reputation: 2042

Automatic create folders in directory c#

I need a script that can create a sub folders automaticaly.

For example:

I have a base folder stored at c:/upload. I tranfer to script folders string: /2011/23/12/3. Script should parse this string and create folders and subfolders in c:/upload like string is (should be c:/upload/2011/23/12/3)

How can I make this?

Now I use if/else and check if folder/subfolder exist, but the script is tooooo big and that is hard to manage.

Upvotes: 7

Views: 15656

Answers (3)

Wills
Wills

Reputation: 11

You can use Directory.CreateDirectory in C# for creating directory.

Upvotes: 1

Aamir
Aamir

Reputation: 15566

Your code to create directories recursively will be as simple as:

Directory.CreateDirectory(path)

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500555

Have you looked at Directory.CreateDirectory, which will create any missing directories along the way?

From the documentation:

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid.

Upvotes: 18

Related Questions