Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a case where recursion doesn't really work, as the list of directories and files always changes as you copy. A better solution is to get the list of all files and folders in advance.</p> <p>You can get all files and directories in a tree using the <a href="http://msdn.microsoft.com/en-us/library/ms143316.aspx" rel="nofollow">Directory.GetFiles(String,String,SearchOption)</a> and <a href="http://msdn.microsoft.com/en-us/library/ms143314.aspx" rel="nofollow">Directory.GetDirectories(String,String,SearchOption)</a> with SearchOption set to <a href="http://msdn.microsoft.com/en-us/library/ms143448.aspx" rel="nofollow">SearchOption.AllDirectories</a>. These will return all files and all directories respectively.</p> <p>You can follow these steps to copy the files:</p> <ol> <li>Get the list of all source directories and sort them in ascending order. This will ensure that parent directories appear before their child directories.</li> <li>Create the target directory structure by creating the child directories in their sorted order. You won't get any path conflicts as the sort order ensures you always create the parent folders before the child folders</li> <li>Copy all the source files to the target directories as before. Order doesn't really matter at this point as the target directory structure already exists.</li> </ol> <p>A quick example:</p> <pre><code> static void Main(string[] args) { var sourcePath = @"c:\MyRoot\TestFolder\"; var targetPath = @"c:\MyRoot\TestFolder\Archive\"; var directories=Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories); var files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories); if(!Directory.Exists(targetPath)) Directory.CreateDirectory(targetPath); foreach (var directory in directories) { var relativePath = GetRelativePath(sourcePath, directory); var toPath = Path.Combine(targetPath, relativePath); if (!Directory.Exists(toPath)) { Directory.CreateDirectory(toPath); } } foreach (var file in files) { var relativePath = GetRelativePath(sourcePath, file); var toPath = Path.Combine(targetPath, relativePath); if (!File.Exists(toPath)) File.Copy(file,toPath); } } //This is a very quick and dirty way to get the relative path, only for demo purposes etc private static string GetRelativePath(string rootPath, string fullPath) { return Path.GetFullPath(fullPath).Substring(rootPath.Length); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload