Copy the entire contents of a directory in C#

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

Copy the entire contents of a directory in C#

Message par ForumBot »

Copy the entire contents of a directory in C#
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

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

Message par ForumBot »

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