Kopieren Sie ein Verzeichnis auf ein anderes Laufwerk

Wie kopiere ich ein Verzeichnis auf ein anderes Laufwerk in C #?

Sie können diesen Code verwenden, um Ihre Operation auszuführen:

public static void CopyAll(DirectoryInfo source, DirectoryInfo target) { // Check if the target directory exists, if not, create it. if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); } // Copy each file into it's new directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@”Copying {0}\{1}”, target.FullName, fi.Name); fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir); } } 

darunter ist auch gut:

  static public void CopyFolder( string sourceFolder, string destFolder ) { if (!Directory.Exists( destFolder )) Directory.CreateDirectory( destFolder ); string[] files = Directory.GetFiles( sourceFolder ); foreach (string file in files) { string name = Path.GetFileName( file ); string dest = Path.Combine( destFolder, name ); File.Copy( file, dest ); } string[] folders = Directory.GetDirectories( sourceFolder ); foreach (string folder in folders) { string name = Path.GetFileName( folder ); string dest = Path.Combine( destFolder, name ); CopyFolder( folder, dest ); } } 

Sie können diese function auch verwenden:

 FileSystem.CopyDirectory(sourceDir, destDir); 
 FileSystem.CopyDirectory(sourceDir, destDir); 

FileSystem.CopyDirectory befindet sich in einem VB-Namespace und einer Assembly, aber das spielt wahrscheinlich keine Rolle.

Gewusst wie: Kopieren, Löschen und Verschieben von Dateien und Ordnern (C # -Programmierhandbuch)
http://msdn.microsoft.com/de-de/library/cc148994.aspx

C # Ordner rekursiv kopieren
http://www.csharp411.com/c-copy-folder-recursively/

Hier ist eine Erweiterung, die in .NET 4.0 und höher funktionieren wird

 var source = new DirectoryInfo(@"C:\Test"); var destination = new DirectoryInfo(@"E:\Test"); source.CopyTo(destination); 

Binden Sie diese Datei in Ihr Projekt ein

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace System.IO { public static class DirectoryInfoExtensions { public static void CopyTo(this DirectoryInfo source, DirectoryInfo target) { if (!target.Exists) target.Create(); foreach (var file in source.GetFiles()) file.CopyTo(Path.Combine(target.FullName, file.Name), true); foreach (var subdir in source.GetDirectories()) subdir.CopyTo(target.CreateSubdirectory(subdir.Name)); } } } 
  private String path; public int copyAllContents(String destinationFolder, ProgressBar progressBar) { int countCopyFiles = 0; if (!Directory.Exists(destinationFolder)) { Directory.CreateDirectory(destinationFolder); } String[] allFilesForCurrentFolder = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); String[] subFoldersAllpath = Directory.GetDirectories(path); for (int i = 0; i < allFilesForCurrentFolder.Length; i++) { try { File.Copy(allFilesForCurrentFolder[i], destinationFolder + "\\" + Path.GetFileName(allFilesForCurrentFolder[i])); countCopyFiles++; progressBar.Value++; } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } } if (subFoldersAllpath.Length == 0) { return allFilesForCurrentFolder.Length; }; for (int i = 0; i < subFoldersAllpath.Length; i++) { this.path = subFoldersAllpath[i]; String[] subFoldersAllpathLastFolder = subFoldersAllpath[i].Split('\\'); countCopyFiles += this.copyAllContents(destinationFolder + "\\" + subFoldersAllpathLastFolder[subFoldersAllpathLastFolder.Length - 1], progressBar); } return countCopyFiles; } 

Hier ist ein Ansatz, der ein Verzeichnis rekursiv als async function kopiert:

 public static async Task CopyDirectoryAsync(string sourceDirectory, string destinationDirectory) { if (!Directory.Exists(destinationDirectory)) Directory.CreateDirectory(destinationDirectory); foreach (var file in Directory.GetFiles(sourceDirectory)) { var name = Path.GetFileName(file); var dest = Path.Combine(destinationDirectory, name); await CopyFileAsync(file, dest); } foreach (var subdir in Directory.GetDirectories(sourceDirectory)) { var name = Path.GetFileName(subdir); var dest = Path.Combine(destinationDirectory, name); await CopyDirectoryAsync(subdir, dest); } } public static async Task CopyFileAsync(string sourceFile, string destinationFile) { using (var sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan)) using (var destinationStream = new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.Asynchronous | FileOptions.SequentialScan)) await sourceStream.CopyToAsync(destinationStream); }