Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I copy a folder and all subfolders and files in .NET? [duplicate]

Possible Duplicate:
Best way to copy the entire contents of a directory in C#

I'd like to copy folder with all its subfolders and file from one location to another in .NET. What's the best way to do this?

I see the Copy method on the System.IO.File class, but was wondering whether there was an easier, better, or faster way than to crawl the directory tree.

like image 497
dthrasher Avatar asked Jun 30 '09 23:06

dthrasher


People also ask

How do I copy a folder and subfolders?

Type "xcopy", "source", "destination" /t /e in the Command Prompt window. Instead of “ source ,” type the path of the folder hierarchy you want to copy. Instead of “ destination ,” enter the path where you want to store the copied folder structure. Press “Enter” on your keyboard.

How do you copy an entire directory structure?

Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.

How do I copy the contents of multiple folders?

If you want to copy the set of files to multiple folders, hold down the CONTROL key while dragging and dropping the file(s). This ensures that the files in the drop stack are not removed once the copy process has completed. This makes it easy to copy the same set of files (from different folders) to multiple locations.

Can you duplicate folders in Windows?

Hold down the Ctrl key on your keyboard and select any files and folders you want to copy. Release the key when you're done. All highlighted files and folders will be copied. Choose Edit and then Copy To Folder from the menu at the top of the folder's window.


2 Answers

Well, there's the VisualBasic.dll implementation that Steve references, and here's something that I've used.

private static void CopyDirectory(string sourcePath, string destPath) {     if (!Directory.Exists(destPath))     {         Directory.CreateDirectory(destPath);     }      foreach (string file in Directory.GetFiles(sourcePath))     {         string dest = Path.Combine(destPath, Path.GetFileName(file));         File.Copy(file, dest);     }      foreach (string folder in Directory.GetDirectories(sourcePath))     {         string dest = Path.Combine(destPath, Path.GetFileName(folder));         CopyDirectory(folder, dest);     } } 
like image 164
Michael Petrotta Avatar answered Sep 28 '22 01:09

Michael Petrotta


Michal Talaga references the following in his post:

  • Microsoft's explanation about why there shouldn't be a Directory.Copy() operation in .NET.
  • An implementation of CopyDirectory() from the Microsoft.VisualBasic.dll assembly.

However, a recursive implementation based on File.Copy() and Directory.CreateDirectory() should suffice for the most basic of needs.

like image 36
Steve Guidi Avatar answered Sep 28 '22 02:09

Steve Guidi