title: How to copy and paste a whole directory to a new path recursively?
body: <p>I want to move a directory as a copy/paste routine, by keeping its structure as it is. I am not looking only for the files within all subfolders in a directory then copy/paste them (as <a href="https://stackoverflow.com/questions/3911595/move-all-files-in-subfolders-to-another-folder-using-c-sharp"><strong>this</strong></a> solution), instead I want to <strong>clone</strong> the whole thing and keep its structure as it is (Tree -> subfolders and files), exactly like a copy and paste routine.</p>
  
  <p>So I found <a href="https://stackoverflow.com/questions/2490619/how-to-copy-a-folder-from-one-one-path-and-paste-it-to-another-path-using-c"><strong>this</strong></a> function that copies a folder full of files to a new path:</p>
  
  <blockquote>
    <p>Folder -> File(s)</p>
  </blockquote>
  
  <p>The function behaves as known as the copy/paste routine. It takes SourcePath, DestinationPath and boolean value to OverWriteExisting. Nice and small but too bad it wasn't marked as the actual answer of that question there (recommend a rate).</p>
  
  <p>But what if I want to move a whole directory? In other words, what if I have a folder that has folders of folders of folders of files and etc? And maybe it is unknown the file structure tree size like this:</p>
  
  <blockquote>
    <p>Folder -> Folder(s) -> ... -> Folder(s) -> File(s)</p>
  </blockquote>
  
  <p>I am using the below routine to copy/paste a folder that has folders. But here I know that I only have one level of folders so only one foreach loop is required:</p>
  
  <pre><code>foreach (var Folder in DestinationFolder) // here I know that I have only one level of folders to reach the files
  {
      CopyDirectory(FolderPath, DestinationPath, false); // use that function to copy the files
  }
  </code></pre>
  
  <p>This above function serves this directory structure:</p>
  
  <blockquote>
    <p>Folder -> Folder(s) -> File(s)</p>
  </blockquote>
  
  <p>I tried <a href="https://stackoverflow.com/questions/3911595/move-all-files-in-subfolders-to-another-folder-using-c-sharp"><strong>this</strong></a> and it didn't do what I want. I only retrieve all files while it searches all the subfolders. Which is not what I want. I want to keep the subfolders and the original structure as it is. Here I get four files instead of the directory structed as it is, subfolders and their subfolders, subfolders, files. Only four because it removes duplicates which I do not want this to happen because I need all of them.</p>
  
  <p>Here is my current structure(but my question is global to any directory):</p>
  
  <blockquote>
    <p>Folder -> Folders -> Folders + Files</p>
  </blockquote>
  
  <p>Here is what the below code does in the new path:</p>
  
  <blockquote>
    <p>NewFolder -> AllFilesFoundInAnySubfolder</p>
  </blockquote>
  
  <pre><code>dialog.FileName = dialog.FileName.Replace(".xml", ""); // get the destination path
  DirectoryInfo dirInfo = new DirectoryInfo(dialog.FileName);
  
  if (dirInfo.Exists == false)
      Directory.CreateDirectory(dialog.FileName);
  
  List&lt;String&gt; EverythingInTheDirectory = Directory
                      .GetFiles(FileStructure.baseSessionPath + "\\" + SelectedSession.Name, "*.*", SearchOption.AllDirectories).ToList(); // source
  
  foreach (string file in EverythingInTheDirectory)
  {
      FileInfo mFile = new FileInfo(file);
      // to remove name collusion
      if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
          mFile.MoveTo(dirInfo + "\\" + mFile.Name);
  }
  </code></pre>
  
  <p><strong>How to move the whole directory with unknown size and keep its structure as it is? Not get only the files from a directory and move them!</strong></p>