c#复制整个目录及文件到新目录

private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
    //创建所有新目录
    foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
    }

    //复制所有文件 & 保持文件名和路径一致
    foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
    }
}

你可能感兴趣的:(c#复制整个目录及文件到新目录)