Similar to the question, I am copying a folder structure from one destination and duplicating it to another. Sorry for posting to an old thread, but it may be useful for someone that ends up here.
Let's assume that we have an application that is standalone, and we need to copy the folder structure from an Input folder to an Output folder. The Input folder and Output folder is in the root directory of our application.
We can make a DirectoryInfo for the Input folder (structure we want to copy) like this:
DirectoryInfo dirInput = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Input\\");
Our output folder location can be stored in a string.
string dirOutput = AppDomain.CurrentDomain.BaseDirectory + "\\Output\\";
This recursive method should handle the rest for us.
public static void ProcessDirectories(DirectoryInfo dirInput, string dirOutput)
{
string dirOutputfix = String.Empty;
foreach (DirectoryInfo di in dirInput.GetDirectories())
{
dirOutputfix = dirOutput + "\\" + di.Name);
if (!Directory.Exists(dirOutputfix))
{
try
{
Directory.CreateDirectory(dirOutputfix);
}
catch(Exception e)
{
throw (e);
}
}
ProcessDirectories(di, dirOutputfix);
}
}
This method can be easily modified to handle files also, but I designed it with only folders in mind for my project.
Now we just call the method with our dirInput and dirOutput.
ProcessDirectories(dirInput, dirOutput);