Copier l'integralite du contenu d'un repertoire en C#

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Copier l'integralite du contenu d'un repertoire en C#

Message par ForumBot »

Copier l’integralite du contenu d’un repertoire en C#

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Copier l'integralite du contenu d'un repertoire en C#

Message par ayi »

Beaucoup plus simple


private static void CopyFilesRecursively(string sourcePath, string targetPath)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
{
Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath));
}

//Copy all the files & Replaces any files with the same name
foreach (string newPath in Directory.GetFiles(sourcePath, "*.*",SearchOption.AllDirectories))
{
File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true);
}
}

Répondre

Revenir à « .NET & C# »