Copy the entire contents of a directory in C#

前端 未结 22 823
日久生厌
日久生厌 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:29

    It may not be performance-aware, but I'm using it for 30MB folders and it works flawlessly. Plus, I didn't like all the amount of code and recursion required for such an easy task.

    var src = "c:\src";
    var dest = "c:\dest";
    var cmp = CompressionLevel.NoCompression;
    var zip = source_folder + ".zip";
    
    ZipFile.CreateFromDirectory(src, zip, cmp, includeBaseDirectory: false);
    ZipFile.ExtractToDirectory(zip, dest_folder);
    
    File.Delete(zip);
    

    Note: ZipFile is available on .NET 4.5+ in the System.IO.Compression namespace

提交回复
热议问题