locked
copy large files and update progressbar RRS feed

  • Question

  • Dear,

    I write some code to copy large files from one location to another location. My code works fine but it updates my progressbar after that the file is completely is copied :(

    Is it possible to update the progressbar with the copy proccess and not when the file is completely copied?

    my code:

                                          

     private void btnStart(object sender, RouteEventArgs e)

    {

    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                backgroundWorker1.RunWorkerAsync();

    }

    private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
            {
                double Size = 0;
                double totalsize = 0;
                double filepercent = 0;

                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                {
                    if(!dir.Exists)
                    {
                        throw new DirectoryNotFoundException("Source directory does not exist or could not be found: "
                            + sourceDirName);
                    }
                    DirectoryInfo[] dirs = dir.GetDirectories();
                    if(!Directory.Exists(destDirName))
                   {
                        Directory.CreateDirectory(destDirName);
                    }

                    FileInfo[] files = dir.GetFiles();
                    int filecount = files.Count();
                    int i = 1;
                   foreach (FileInfo fi in files)
                    {
                       totalsize += fi.Length;
                    }
                    totalsize = Math.Round((totalsize / 1048576), 2);

                    foreach (FileInfo file in files)
                    {
                        Size += Math.Round(((double)file.Length / 1048576), 2);
                        filepercent = Size * 100 / totalsize;
                        string temppath = System.IO.Path.Combine(destDirName, file.Name);
                        file.CopyTo(temppath, true);


                        backgroundWorker1.ReportProgress((int)filepercent, "Files " + i + "/" + filecount + " " + Size + "/" + totalsize + " Mb");
                        Thread.Sleep(100);
                        i++;
                    }
                }
            }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                progressBar.Value = e.ProgressPercentage;
                lblText.Content = e.UserState as string + "  " + "Please wait....";
            }

    Wednesday, April 4, 2018 12:53 PM

Answers

  • You're using FileInfo.CopyTo so no. That call is blocking until the copy is complete. If you want to copy a file with progress you're going to have to use P/Invoke to call out to CopyFileEx (or even CopyFile2) which does support progress notifications while copying files.

    Alternatively you can go the (slower) route of manually copying the files yourself by opening the file for reading and stream the contents to the target file. Since you are reading the data you can update the progress as you go along.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by JDECLE02 Thursday, April 5, 2018 5:46 PM
    Wednesday, April 4, 2018 2:41 PM

All replies

  • You're using FileInfo.CopyTo so no. That call is blocking until the copy is complete. If you want to copy a file with progress you're going to have to use P/Invoke to call out to CopyFileEx (or even CopyFile2) which does support progress notifications while copying files.

    Alternatively you can go the (slower) route of manually copying the files yourself by opening the file for reading and stream the contents to the target file. Since you are reading the data you can update the progress as you go along.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by JDECLE02 Thursday, April 5, 2018 5:46 PM
    Wednesday, April 4, 2018 2:41 PM
  • You can also P/Invoke IFileOperation

    (but a lot of declarations... There was a sample from MSDN Mag)



    • Edited by Castorix31 Wednesday, April 4, 2018 3:24 PM
    Wednesday, April 4, 2018 3:13 PM