[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Make folders in C#

[Make folders in C#]

Recently I needed to make folders for a project. It probably would have taken two or three minutes, so to save time I spent an hour writing this program.

It's pretty straightforward. Enter the name of the parent folder in the Parent Diretcory text box. Then enter a list of subfolder names in the Folder Names text box and click Create to execute the following code.

private void btnCreate_Click(object sender, EventArgs e) { string parent = txtParent.Text.Trim(); int num_created = 0; foreach (string folder in txtFolders.Lines) { string folder_name = folder.Trim(); if (folder_name.Length > 0){ string full_name = Path.Combine(parent, folder); Directory.CreateDirectory(full_name); num_created++; } } MessageBox.Show("Created " + num_created + " folders.", "Folders Created"); }

This code gets the parent folder's name and then loops through the lines in the subfolders text box. It uses Path.Combine to compose the subfolder's full name and then uses Directory.CreateDirectory to make it.

That's all there is to it. If you're going to create dozens of subfolders, you might want to add error handling so you can decide whether to stop or continue the loop if there's a problem.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.