Copy all files in directory

You can’t. Neither Directory nor DirectoryInfo provide a Copy method. You need to implement this yourself.

void Copy(string sourceDir, string targetDir)
{
    Directory.CreateDirectory(targetDir);

    foreach(var file in Directory.GetFiles(sourceDir))
        File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));

    foreach(var directory in Directory.GetDirectories(sourceDir))
        Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}

Please read the comments to be aware of some problems with this simplistic approach.

Leave a Comment