How to Copy folder structure in C# – Programming, Pseudocode Example, C# Programming Example
C# Windows Form

How to Copy folder structure in C#

Sometimes I need to copy a folder structure from one directory to another. For example, for my business I have directories representing years. Inside those directories I have standard folders for payroll, schedules, accounting, and others. With the new year, I needed to duplicate that folder structure. Any normal person would simply create the half dozen new folders and be done. Being a software developer, I needed to write a program to copy folder structure for me.

Enter the name of the folder that contains the subfolders that you want to duplicate and click List Folders to see the folders that are available. Check the ones that you want to duplicate, enter the path to the folder that should contain the copies, and then click Make Subfolders to copy the folders. This only copies the top-level folders. It does not copy the files within those folders or any subfolders that they may contain.

List Subfolders

When you click List Subfolders, the following code executes.

This code clears the CheckedlistBox control. It then gets the directory name that you entered and uses the Directory class’s GetDirectories method to loop through the folders contained in that directory.

For each subfolder, the code creates a DirNameInfo object and adds it to the listbox. The following code shows that class.

The DirNameInfo class simply holds a folder’s short and long names. Its overridden ToString method makes the listbox display the folder’s short name.

After it finishes adding the folder names to the listbox, the btnListSubfolders_Click event handler uses the following CheckAll extension method to check all of the listbox’s items.

Copy Folder Structure

When you click Make Subfolders, the following code executes.

This code loops through the checked DirNameInfo objects in the listbox. For each checked object, it calls the following CreateSubfolder method. If that method returns false to indicate a failure, then the code breaks out of the loop so you can stop early rather than continuing the loop.

The CreateSubfolder method first checks whether the folder already exists. If it does exist, the code tells you and asks if you would like to continue. If you click No, the method returns false so the calling code breaks out of its loop.

If the folder does not already exist, the code uses the Directory class’s CreateDirectory method to create it.

If there is some other error, the try ... catch block catches it, displays the exception message, and gives you a chance to exit the subfolder-creation loop.


I admit that you may not need to copy folder structure often, but if you do, this program may help. The example also demonstrates a couple of useful techniques such as using a class to display strings (short folder names) in a list control, making extension methods to check and uncheck all of a list’s items, and allowing a method to break out of a loop in the calling method.

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


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.