gpt4 book ai didi

c# - 带有进度条报告的 stream.copyto

转载 作者:太空狗 更新时间:2023-10-30 00:24:40 25 4
gpt4 key购买 nike

我想合并 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com