Click here to Skip to main content
15,889,116 members
Articles / Programming Languages / C#
Tip/Trick

Recursively Copy folder contents to another in C#

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
6 Nov 2011CPOL 51.9K   13   4
How to recursively copy folder contents to another in C#.
C#
private bool CopyFolderContents(string SourcePath, string DestinationPath)
{
   SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
   DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
 
   try
   {
      if (Directory.Exists(SourcePath))
      {
         if (Directory.Exists(DestinationPath) == false)
         {
            Directory.CreateDirectory(DestinationPath);
         }

         foreach (string files in Directory.GetFiles(SourcePath))
         {
            FileInfo fileInfo = new FileInfo(files);
            fileInfo.CopyTo(string.Format(@"{0}\{1}", DestinationPath, fileInfo.Name), true);
         }

         foreach (string drs in Directory.GetDirectories(SourcePath))
         {
            DirectoryInfo directoryInfo = new DirectoryInfo(drs);
            if (CopyFolderContents(drs, DestinationPath + directoryInfo.Name) == false)
            {
               return false;
            }
         }
      }
      return true;
   }
   catch (Exception ex)
   {
      return false;
   }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.

Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.

Interests:
I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .

I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

Comments and Discussions

 
GeneralThere is no need to add \ on SourcePath and DestinationPath ... Pin
Ivan Ičin8-Nov-11 8:46
Ivan Ičin8-Nov-11 8:46 
GeneralIf the SourcePath do not exists, it returns true. I think th... Pin
InCodeVB7-Nov-11 22:05
InCodeVB7-Nov-11 22:05 
QuestionYour string.Concat benchmark is WRONG!!! Pin
motorboy7914-Nov-11 20:55
motorboy7914-Nov-11 20:55 
Take a look at the compiled assembly in Reflector, or ILSpy!

Your string concat test does not do any string operations (just incrementing the loop variable), because the optimization at compile time cuts it out, BECAUSE THE RESULT STRING IS NOT USED ANYWHERE!

If you rewrite your code to use the resulting string for something quic operation and take another look at the compiled code, you can see, the optimizer converted your 3 string concatenations into one constant string, because it does not use any runtime variable data.

So if you do some comparing code, use some fix length, runtime generated string instances and run the benchmark again!

You will be suprised how efficient the StringBuilder is for more than 2 appends!

Rule of thumb:
if you concat only 2 strings --> you can use string.Concat
if you concat more than 2 strings --> use StringBuilder !!! (or string.Format)
(string.Format uses StringBuilder internally)

StringBuilder is not just faster (because it use less memory copy) but it makes your used memory less fragmented, so it does not make so much work for the GC Smile | :)
AnswerRe: Your string.Concat benchmark is WRONG!!! Pin
Kabwla.Phone29-Nov-11 6:22
Kabwla.Phone29-Nov-11 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.