C# 具有进度条报告的stream.copyto

C# 具有进度条报告的stream.copyto,c#,C#,我想合并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

我想合并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));
        }
    }
}

这是我最后使用的代码谢谢patrick的帮助

            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");
List files=newlist();
if(file1!=null&&file2!=null)
{
文件。添加(文件1);
文件。添加(文件2);
}
如果(file3!=null)
{
文件。添加(文件3);
}
如果(file4!=null)
{
文件。添加(文件4);
}
max=files.Count;
MessageBox.Show(“合并已启动”);
使用(流输出=File.OpenWrite(dest))
{
foreach(文件中的字符串inputFile)
{
使用(流输入=File.OpenRead(inputFile))
{
字节[]缓冲区=新字节[32*1024];
int-read;
而((read=input.read(buffer,0,buffer.Length))>0)
{
输出。写入(缓冲区,0,读取);
计数++;
//汇报进展
进度=计数*100/读取;
backgroundWorker2.ReportProgress(转换为32.progress);
}
}
}
}
MessageBox.Show(“合并完成”);

它不工作占用应用程序可能会导致我试图合并4个2gb文件,使其成为18 gbfile@outlaw1994:我合并了你的代码和我的代码。谢谢,我测试了它,它们合并得很好,实际上比我上一个代码更快,但是进度马上就要100%了。要解决这个问题,还必须将ms改为outputFeel free添加它作为未来用户的答案。