1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| private void CopierDossier(string sourcePath, string destPath, bool overwrite)
{
log.Info(string.Format("CopierDossier : copie du dossier {0} vers {1}", sourcePath, destPath));
System.IO.DirectoryInfo sourceDir = new System.IO.DirectoryInfo(sourcePath);
System.IO.DirectoryInfo destDir = new System.IO.DirectoryInfo(destPath);
if (sourceDir.Exists)
{
if (!destDir.Exists)
destDir.Create();
// copie des fichiers
foreach (System.IO.FileInfo file in sourceDir.GetFiles())
{
try
{
if (overwrite)
file.CopyTo(System.IO.Path.Combine(destDir.FullName, file.Name), true);
else if ((System.IO.File.Exists(System.IO.Path.Combine(destDir.FullName, file.Name))) == false)
file.CopyTo(System.IO.Path.Combine(destDir.FullName, file.Name), false);
}
catch (Exception e)
{
log.Error(string.Format("Erreur lors de la copie de {0} vers {1}", file.FullName, System.IO.Path.Combine(destDir.FullName, file.Name)), e);
throw e;
}
}
// copie des dossiers
foreach (System.IO.DirectoryInfo dir in sourceDir.GetDirectories())
{
CopierDossier(dir.FullName, System.IO.Path.Combine(destDir.FullName, dir.Name), overwrite);
}
}
} |
Partager