/// <summary>
/// 复制文件夹中的所有内容
/// </summary>
/// <param name="sourceDirPath">源文件夹目录</param>
/// <param name="targetDirPath">目标文件夹目录</param>
public static void CopyDirectory(string sourceDirPath, string targetDirPath)
{
try
{
//如果文件夹不存在,则创建
if (!Directory.Exists(targetDirPath)) { Directory.CreateDirectory(targetDirPath); }
string[] files = Directory.GetFiles(sourceDirPath);
foreach (string file in files)
{
var fullPath = Path.Combine(targetDirPath, Path.GetFileName(file));
Console.WriteLine(fullPath);
File.Copy(file, fullPath, true);
}
string[] dirs = Directory.GetDirectories(sourceDirPath);
foreach (string dir in dirs)
{
//递归
CopyDirectory(dir, Path.Combine(targetDirPath, Path.GetFileName(dir)));
}
}
catch (Exception ex)
{
Console.WriteLine("error= " + ex);
}
}