History

History of .net

Thursday, 24 January 2013

Method to copy files and folder from one Folder to another Folder

private bool CopyFolderContents(string SourcePath, string DestinationPath)
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

try
{
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
{
Directory.CreateDirectory(DestinationPath);
}

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;
}
}

No comments:

Post a Comment