How to copy folder with content with C#

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)
{

}
}
}
}
Please share, if it is helpfulShare on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Email this to someonePrint this page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>