Copy the entire contents of a directory in C#

前端 未结 22 1248
日久生厌
日久生厌 2020-11-22 07:13

I want to copy the entire contents of a directory from one location to another in C#.

There doesn\'t appear to be a way to do this using System.IO class

22条回答
  •  攒了一身酷
    2020-11-22 07:27

    One variant with only one loop for copying of all folders and files:

    foreach (var f in Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories))
    {
        var output = Regex.Replace(f, @"^" + path, newPath);
        if (File.Exists(f)) File.Copy(f, output, true);
        else Directory.CreateDirectory(output);
    }
    

提交回复
热议问题