Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stream.copyto with progress bar reporting

Tags:

c#

i want to merge 2 large files but atm my code only updates the progress after 1 file is copied is there a better way to report progress this is my copy code atm

 max = files.Count;
 MessageBox.Show("Merge Started");
 using (Stream output = File.OpenWrite(dest))
   {
      foreach (string inputFile in files)
        {
          using (Stream input = File.OpenRead(inputFile))
            {
              input.CopyTo(output);
              count++;
              progress = count * 100 / max;
              backgroundWorker2.ReportProgress(Convert.ToInt32(progress));
            }
        }
  }
MessageBox.Show("Merge Complete");
like image 520
outlaw1994 Avatar asked Apr 04 '14 08:04

outlaw1994


2 Answers

You could read the file in chunks.

You should notify the BackgroundWorker in between.

using (Stream output = File.OpenWrite(dest))
{
    foreach (string inputFile in files)
    {
        using (Stream input = File.OpenRead(inputFile))
        {
            byte[] buffer = new byte[16 * 1024];

            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);

                // report progress back
                progress = (count / max + read / buffer.Length /* part of this file */) *  100;
                backgroundWorker2.ReportProgress(Convert.ToInt32(progress));
            }

            count++;
            progress = count * 100 / max;
            backgroundWorker2.ReportProgress(Convert.ToInt32(progress));
        }
    }
}
like image 103
Patrick Hofman Avatar answered Oct 30 '22 15:10

Patrick Hofman


this is the code i ended up using thanks patrick for helping a lot

            List<string> files = new List<string>();
            if (file1 != null && file2 != null)
            {
                files.Add(file1);
                files.Add(file2);
            }
            if (file3 != null)
            {
                files.Add(file3);
            }
            if (file4 != null)
            {
                files.Add(file4);
            }
                    max = files.Count;
                    MessageBox.Show("Merge Started");
                    using (Stream output = File.OpenWrite(dest))
                    {
                        foreach (string inputFile in files)
                        {
                            using (Stream input = File.OpenRead(inputFile))
                            {
                                byte[] buffer = new byte[32 * 1024];
                                int read;
                                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, read);
                                    count++;
                                    // report progress back
                                    progress = count * 100 / read;
                                    backgroundWorker2.ReportProgress(Convert.ToInt32(progress));
                                }
                            }
                        }
                    }
                    MessageBox.Show("Merge Complete");
like image 20
outlaw1994 Avatar answered Oct 30 '22 13:10

outlaw1994