-1

I'm using below code to make a copy a folder available on a network. This folder has, subfolders and files with total of 455 files and 13 folders and of size 409 MB.

My method is recursively calling itself to create a copy of sub folders and files in it. Overall, this method is taking more than 10 minutes to finish the task and I'm looking to speed up the process.

So far, I've went through different posts but did not find any better solution. Is there a better way to achieve my task or any improvements to my code for a faster execution?

void CopyDirectoryAndFiles(string sourceDirectory, string destinationDirectory, bool recursive)
        {
            // Get information about the source directory
            var dir = new DirectoryInfo(sourceDirectory);

            // Check if the source directory exists
            if (!dir.Exists)
                throw new DirectoryNotFoundException($"Source directory not found:dir.FullName}");

            // Cache directories before we start copying
            DirectoryInfo[] dirs = dir.GetDirectories();

            // Create the destination directory
            Directory.CreateDirectory(destinationDirectory);

            // Get the files in the source directory and copy to the destination directory
            foreach (FileInfo file in dir.GetFiles())
            {
                string targetFilePath = Path.Combine(destinationDirectory, file.Name);
                file.CopyTo(targetFilePath);
            }

            // If recursive and copying subdirectories, recursively call this method
            if (recursive)
            {
                foreach (DirectoryInfo subDir in dirs)
                {
                   string newDestinationDirectory= Path.Combine(destinationDirectory,subDir.Name);
                    CopyDirectoryAndFiles(subDir.FullName, newDestinationDirectory, true);
                }
            }
        }

Thanks for your help.

Satish
  • 11
  • 5
  • 1
    According to this not. https://stackoverflow.com/questions/32236796/fastest-way-to-copy-files-from-one-directory-to-another But you could maybe improve performance by changing the caching or defragmantation of the harddrive. Or switching to SSD or even use a RAID drive setup. (see also: https://www.diskpart.com/articles/how-to-speed-up-hdd-performance-0725.html) – Wouter Jun 26 '22 at 22:57
  • Does this answer your question? [How to bring up the built-in File Copy dialog?](https://stackoverflow.com/questions/6687443/how-to-bring-up-the-built-in-file-copy-dialog) – John Alexiou Jun 27 '22 at 01:58

1 Answers1

0

I don't think there's a way to increase performance in a drastic way, but I can suggest a few things to try:

  • replace foreach w/ Parallel.ForEach to copy data in several streams;
  • you can use an external tool (e.g. xcopy), which is optimized for the task, and call this tool from your C# code. xcopy can copy folders recursively if you specify the /e flag.
umberto-petrov
  • 731
  • 4
  • 8
  • 2
    I would be carefull with the parallel foreach and test this. E.g. reading data from different sectors or the cache might have a lot of misses. – Wouter Jun 26 '22 at 23:06
  • 1
    I'm with @Wouter. If you are reading from a rotating disk, doing things in parallel will likely mess up the caching. I don't know enough about SSDs to comment on that. How does the timing compare to using Windows Explorer, or using `copy` or `xcopy`from the command line – Flydog57 Jun 26 '22 at 23:39
  • agree in general, but I think the author mentioned that he's reading from the network – umberto-petrov Jun 27 '22 at 23:07