Copier coller un répertoire en C# (Copy-Paste folder)

Tags : Copier coller, c#, ASP.NET

Categories: C#

Copier le contenu d'un répertoire en utilisant la récursivité :

private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
   // Check if the target directory exists, if not, create it.
   if (Directory.Exists(target.FullName) == false)
   {
       Directory.CreateDirectory(target.FullName);
   }
   // Copy each file into the new directory.
   foreach (FileInfo fi in source.GetFiles())
   {
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
   }
   // Copy each subdirectory using recursion.
   foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
   {
       DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
       CopyAll(diSourceSubDir, nextTargetSubDir);
   }
}

Aucun commentaire

Ajouter un commentaire