Copy Directory Performance

I use C # to copy files from one directory to another. I use the code from msdn, but it takes quite a minute or so to copy a couple of gigs. It only takes a few seconds in explorer. http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C Awfully there is a faster way. :)

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 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); } } 

Using Parallel, I was able to copy 6 games per minute faster than explorer and xcopy.


 private static void CopyAll(string SourcePath, string DestinationPath) { string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories); Parallel.ForEach(directories, dirPath => { Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); }); string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories); Parallel.ForEach(files, newPath => { File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath)); }); } 
0
source share
4 answers

What you use is recursion. It always slows down the system.

Use this as it has no recursion.

 void CopyAll (string SourcePath, string DestinationPath) { //Now Create all of the directories foreach (string dirPath in Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories)) Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); //Copy all the files foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories)) File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath)); } 
+4
source

Your problem is * rewriting the file. I see you calling

 fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); 

where true means overwriting the file if it exists.

Based on this, I believe that in your application you can overwrite existing files.

Material should go faster if you

  • rename the destination directory first with some temporary unique name
  • after just copy the source to dest (so don't rewrite)

if you manage to delete the temporary directory, if the failure deletes all the copied files and renames temp to the original name.

To be faster.

Good luck.

0
source

I suspect @Oded is correct and you are comparing a copy to a move.

If you want to make sure that what you are doing is the same as in the shell, you can look at SHFileOperation or (on Vista or later) IFileOperation . At least as far as I know, you have to use SHFileOperation through P / Invoke. IFileOperation is a COM interface.

0
source

There is a big problem with using Parallel.ForEach loops with creating directories, the first thing you need to know is the subdirectories nested in other directories, if Parallel creates the directories out of order, the code may be called due to an attempt to create level 8 directories , while level 6 directories have not yet been completed.

0
source

Source: https://habr.com/ru/post/914572/


All Articles