Hello,

  Here is a small c# method that manage copy of folders. The code is documented enough so I won’t give more details Clignement d'œil do not hesitate to use comments if any questions/remarks!

/// <summary>
/// Copy full folder to another
/// </summary>
/// <param name="sourceFolder">Source of files/folders</param>
/// <param name="destinationFolder">Destination</param>
/// <param name="filter">Filtering to get files, default is *.*</param>
/// <param name="overwriteDestinationFolder">Allow to drop destination folder before copy, if true next parameter is useless. Default is true.</param>
/// <param name="overwriteDestinationFiles">Allow to overwrite files that already exists in destination folder. Default is true, used only if [overwriteDestinationFolder] is false.</param>
/// <remarks>If errors occurs during any step of copy (overwriting open files, deleting locked folders, access denied operations, ...) 
/// this method will just rethrow the exceptions.</remarks>
private void CopyFolder(string sourceFolder, string destinationFolder, string filter = "*.*", bool overwriteDestinationFolder = true, bool overwriteDestinationFiles = true)
{
  //HACK: System.IO.Path.Combine lead to strange results when folders starts and/or ends with '\'
  sourceFolder = sourceFolder.Trim('\\');
  destinationFolder = destinationFolder.Trim('\\');
  if (overwriteDestinationFolder && System.IO.Directory.Exists(destinationFolder))
  {
    try
    {
      System.IO.Directory.Delete(destinationFolder, true);
      //The delete command will send the delete to the OS.
      //Recreating the directory can throw exception because it may not be already deleted!
      while (System.IO.Directory.Exists(destinationFolder))
        System.Threading.Thread.Sleep(1);
    }catch
    {
      //unable to drop the folder, try deleting content
      System.IO.Directory.GetDirectories(destinationFolder).ToList().ForEach(t =>
      {
        System.IO.Directory.Delete(t, true);
        while (System.IO.Directory.Exists(t)) System.Threading.Thread.Sleep(1);
      });
      System.IO.Directory.GetFiles(destinationFolder).ToList().ForEach(t =>
      {
        System.IO.File.Delete(t);
      });
    }
  }
  if (!System.IO.Directory.Exists(destinationFolder))
    System.IO.Directory.CreateDirectory(destinationFolder);
  foreach (var fld in System.IO.Directory.GetDirectories(sourceFolder))
  {
    //HACK : the GetFileName returns the last part of the path from the latest '\'. This allows to get the last "folder" in the folder tree :)
    //this : System.IO.Path.Combine(destinationFolder, System.IO.Path.GetFileName(fld))
    //will gives the new destination folder for recursive call
    //NOTE : if overwrite of destination folder is true, it has already been cleaned so no needs to send this again for subfolders
    CopyFolder(fld, System.IO.Path.Combine(destinationFolder, System.IO.Path.GetFileName(fld)), filter, false, overwriteDestinationFiles);
  }
  foreach (var file in System.IO.Directory.GetFiles(sourceFolder, filter))
    System.IO.File.Copy(file, System.IO.Path.Combine(destinationFolder, System.IO.Path.GetFileName(file)), overwriteDestinationFiles);
}

 

  Hope this could help!

 

Version Française : ici