我想合并 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));
}
}
}
关于c# - 带有进度条报告的 stream.copyto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857713/