Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective copying multiple files

Tags:

c#

.net

file

copy

I have to copy quite a lot of files from one folder to another. Currently I am doing it in this way:

string[] files = Directory.GetFiles(rootFolder, "*.xml");
foreach (string file in files)
{
    string otherFile = Path.Combine(otherFolder, Path.GetFileName(file));
    File.Copy(file, otherFile);
}

Is that the most efficient way? Seems to take ages.

EDIT: I am really asking if there is a faster way to do a batch copy, instead of copying individual files, but I guess the answer is no.

like image 347
Grzenio Avatar asked Oct 21 '08 16:10

Grzenio


People also ask

How do I copy and paste a bunch of files?

Click one, then, while holding down the Ctrl key, click all the others, one at a time. The right-click one them and choose Cut. Finally, go to the new folder, right-click there, and choose Paste.

Which is the easiest method for copying and moving files?

Method 1: Right-click Go to the location where stores your file or folder (hard drive, USB, etc.). Click the name of the file or folder you wish to copy. Right-click the highlighted file or folder and click Copy. Go to the destination folder, right-click the destination folder and click Paste.


1 Answers

I can't think of a more efficient way than File.Copy, it goes directly to the OS.

On the other hand if it takes that long, I would strongly suggest to show a progress dialog - like SHFileOperation does it for you. At least your users will know what is happening.

like image 145
liggett78 Avatar answered Sep 20 '22 17:09

liggett78