簡體   English   中英

stream.copyto與進度條報告

[英]stream.copyto with progress bar reporting

我想合並2個大文件,但atm我的代碼只更新1個文件復制后的進度是否有更好的方法來報告進度這是我的復制代碼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");

您可以以塊的形式讀取文件。

您應該在兩者之間通知BackgroundWorker

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));
        }
    }
}

這是我最終使用感謝帕特里克幫助了很多的代碼

            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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM