-1

I am trying to copy a folder from a source to a destination. The source and destination folder will be in the same directory. The structure of the folder will vary each copy, so it needs to be generic. The folder will contain files, and subdirectories that need to be copied over. So all in all the entire contents of one folder should be copied no matter what it contains to another folder.

I hope this isnt too vauge. But just a quick example of what i'm looking for: Source folder path: c:\directories\versions\11.0.2 Destination folder path: c:\directories\versions\11.0.3

copy all of 11.0.2 contents into 11.0.3

Here is the code I currently have that isn't effective:

//Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourceDir, "*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(sourceDir, targetDir));

//Copy all the files
foreach (string newPath in Directory.GetFiles(sourceDir, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(sourceDir, targetDir));

Any ideas of how to accomplish this?

user2619395
  • 249
  • 1
  • 6
  • 15
  • What do you mean when you say "that isn't effective"? – Zeph Aug 19 '13 at 18:24
  • In what way is what you have not effective? What does it currently do? – Michelle Aug 19 '13 at 18:24
  • Duplicate: http://stackoverflow.com/questions/7146021/copy-all-files-in-directory – rie819 Aug 19 '13 at 18:25
  • possible duplicate of [Copying Files Recursively](http://stackoverflow.com/questions/7064864/copying-files-recursively) – DontVoteMeDown Aug 19 '13 at 18:25
  • @Zeph and Michelle What doesnt work is some of the subdirectories contain subdirectories of their own. However if its just simply files in the subdirectories it works. But copying the subdirectories in subdirectories in a directory (getting a bit inception here) doesnt work. – user2619395 Aug 19 '13 at 18:26
  • [How to: Copy Directories](http://msdn.microsoft.com/en-us/library/bb762914.aspx) – Jim Mischel Aug 19 '13 at 18:27

1 Answers1

0

Well this is the MSDN example

This example demonstrates how to use I/O classes to synchronously copy the contents of a directory to another location. In this example, the user can specify whether to also copy the subdirectories. If the subdirectories are copied, the method in this example recursively copies them by calling itself on each subsequent subdirectory until there are no more to copy.

You could also have a look at

Asynchronous File I/O

Asynchronous operations enable you to perform resource-intensive I/O operations without blocking the main thread. This performance consideration is particularly important in a Windows Store app or desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working.

Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284