How to copy folder with content using C#

less than 1 minute read

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CopyFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourcePath = @"G:\Mahedee_Share\Created";
            string destPath = @"G:\Mahedee_Share\Development";
            CopyFolder(sourcePath,destPath);
        }
        
        private static void CopyFolder(string sourcePath, string destinationPath)
        {
        
            try
            {
            
            //Now Create all of the directories
            foreach (string dirPath in Directory.GetDirectories(sourcePath, "*",
            SearchOption.AllDirectories))
            Directory.CreateDirectory(dirPath.Replace(sourcePath, destinationPath));
            
            //Copy all the files & Replaces any files with the same name
            foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",
            SearchOption.AllDirectories))
            System.IO.File.Copy(newPath, newPath.Replace(sourcePath, destinationPath), true);
            }
            catch (Exception exp)
            {
            
            }
        }
    }
}