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.

1 comment:

SteveBrad said...

Hi, thank you for sharing this great info. Was just browsing through the net in my office and happened upon your blog. It is really very well written and quit comprehensive in explaining with a very simple language.
Online Generic Viagra
Generic Viagra Online