Copy the entire contents of a directory in C#

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Copy the entire contents of a directory in C#

Message par Clippy »

Copy the entire contents of a directory in C#
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Copy the entire contents of a directory in C#

Message par Clippy »

Much easier

```
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# »