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.
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.
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.
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.
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.
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); } }
Michal Talaga references the following in his post:
However, a recursive implementation based on File.Copy()
and Directory.CreateDirectory()
should suffice for the most basic of needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With