Copy All Directories with Files using C#

Here is the sample code for copying a directory with sub directories and all the files using C#.


public static void CopyAllDirectories(DirectoryInfo source, DirectoryInfo target)
{
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

foreach (FileInfo fileInfo in source.GetFiles())
{ fileInfo.CopyTo(Path.Combine(target.ToString(),fileInfo.Name), true);
}
foreach (DirectoryInfo sourceSubDir in source.GetDirectories())
{
DirectoryInfo targetSubDir =
target.CreateSubdirectory(sourceSubDir.Name);
CopyAllDirectories (sourceSubDir, targetSubDir);
}
}


...S.VinothkumaR.

No comments: