public static Task CopyDirectoryAsync(string source, string destination, Progress progress) => Task.Run(() => { progress.UpdateInstall(0); var total = Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories).Count(); var current = 0; CopyDir(source, destination, progress, total, ref current); });
// From http://stackoverflow.com/questions/677221/copy-folders-in-c-sharp-using-system-io // static void CopyDir(string source, string destination, Progress progress, int total, ref int current) { var src = new DirectoryInfo(source); var dest = new DirectoryInfo(destination); if (!dest.Exists) { dest.Create(); } // Copy all files var files = src.GetFiles(); foreach (var file in files) { file.CopyTo(Path.Combine(dest.FullName, file.Name), true); current++; progress.UpdateInstall((int)Math.Round((double)current / total * 100.0)); } // Process subdirectories var dirs = src.GetDirectories(); foreach (var dir in dirs) { // Get destination directory var destinationDir = Path.Combine(dest.FullName, dir.Name); // Call CopyDirectory() recursively. CopyDir(dir.FullName, destinationDir, progress, total, ref current); } }