How to create a recursive function to copy all files and folders

后端 未结 4 1215
日久生厌
日久生厌 2020-12-11 04:19

I am trying to create a function that will recursively copy a source folder and all files and folders inside of it to a different location.

At the moment, I have to

4条回答
  •  遥遥无期
    2020-12-11 04:53

    take a look at my question:

    performance of copying directories i used parallel foreach and it is very fast

    private static void CopyAll(string SourcePath, string DestinationPath)
    {
    
    string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories);
    
    Parallel.ForEach(directories, dirPath =>
    {
        Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
    }); 
    
    string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
    
    Parallel.ForEach(files, newPath =>
    {
        File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
    }); 
    }
    

提交回复
热议问题