Note that there are some explanatory texts on larger screens.

plurals
  1. POperformance of copying directories
    text
    copied!<p>I am using C# to copy files from one directory to another directory. I am using code from msdn but its pretty slow taking a minute or so to copy a couple of gigs. It only takes seconds in explorer. <a href="http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C" rel="nofollow">http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C</a> Surly there a faster way..:)</p> <pre><code> private static void Copy(string sourceDirectory, string targetDirectory) { DirectoryInfo diSource = new DirectoryInfo(sourceDirectory); DirectoryInfo diTarget = new DirectoryInfo(targetDirectory); CopyAll(diSource, diTarget); } private 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()) { 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); } } </code></pre> <hr> <p>Using Parallel I was able to copy 6gigs in under a minute faster than explorer and xcopy. </p> <hr> <pre><code>private static void CopyAll(string SourcePath, string DestinationPath) { string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories); Parallel.ForEach(directories, dirPath =&gt; { Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); }); string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories); Parallel.ForEach(files, newPath =&gt; { File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath)); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload