user5193098
user5193098

Reputation:

If copying files from and to network drive location with my C# tool, it is EXTREMELY SLOW, why?

In my tool, I built in a feature that copies files from user specified location to another one. If I copy from and copy to network drive based location, then a single 2 kb file takes about 10 minutes to copy. It is not a network issue, as doing it without the tool is working properly.

Expectation: tool to copy files from one network location to another.

What happens: a single 1 kb file takes more than 10 minutes to copy. If I do it from local drive to network location, or vica versa, it is better.

Here is my code for copying:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (!string.IsNullOrWhiteSpace(fedPREVIEW.Text))
    if (!string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
    if (!string.IsNullOrWhiteSpace(foldercreationPATH.Text))
    {  

        for (int i = 1; i <= 100; i++)
        {
            string sourcePath = @pathofsrcfilesTOCOPY.Text;
            string targetPath = foldercreationPATH.Text + "\\01_SR";
            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            //Copy the folders from sourcepath and place into mentioned target path, 
            //Overwrite the folder if same file is exist in target path
            foreach (string srcPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories))
            {
                Directory.CreateDirectory(srcPath.Replace(sourcePath, targetPath));
            }

            //Copy the file from sourcepath and place into mentioned target path, 
            //Overwrite the file if same file is exist in target path + it also copies subfolders and files from folders.
            foreach (var srcPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories))
            {
                System.IO.File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
            }

            // Report progress.
            backgroundWorker1.ReportProgress(i);
        }
    }
}

Upvotes: 0

Views: 1033

Answers (1)

Jcl
Jcl

Reputation: 28272

You are doing the same thing 100 times here (i.e., "copying a file" by what you say this is doing, 100 times).

But you say the problem is copying one file, while the provided source shows a complete path replication (creating the directories then copying all files... 100 times!).

Just debug your code and see what it is doing... if a single System.IO.File.Copy operation for a 2kb file takes 10 minutes, then your question is right... as you are posting it, either the question is wrong, or your measurements are, since your code is doing much more than copying a file (... and it's doing it 100 times!)

... 100 times!

Upvotes: 4

Related Questions