Click here to Skip to main content
11,409,450 members (61,133 online)
Click here to Skip to main content

Tagged as

Recursively Copy folder contents to another in C#

, 6 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to recursively copy folder contents to another in C#.
private bool CopyFolderContents(string SourcePath, string DestinationPath)
   SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
   DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
      if (Directory.Exists(SourcePath))
         if (Directory.Exists(DestinationPath) == false)
         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;


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


About the Author

aspnet -i
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.

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 ... Pinmemberivanicin8-Nov-11 9:46 
GeneralIf the SourcePath do not exists, it returns true. I think th... PinmemberInCodeVB7-Nov-11 23:05 
QuestionYour string.Concat benchmark is WRONG!!! Pinmembermotorboy7914-Nov-11 21:55 
AnswerRe: Your string.Concat benchmark is WRONG!!! PinmemberKabwla.Phone29-Nov-11 7:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150414.5 | Last Updated 6 Nov 2011
Article Copyright 2011 by aspnet -i
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid