您必须编写自己的方法,也许作为扩展方法。它可能看起来像这样:
public static async Task CopyToWithProgressAsync(this Stream source,
Stream destination,
int bufferSize = 4096,
Action<long> progress = null)
{
var buffer = new byte[bufferSize];
var total = 0L;
int amtRead;
do
{
amtRead = 0;
while(amtRead < bufferSize)
{
var numBytes = await source.ReadAsync(buffer,
amtRead,
bufferSize - amtRead);
if(numBytes == 0)
{
break;
}
amtRead += numBytes;
}
total += amtRead;
await destination.WriteAsync(buffer, 0, amtRead);
if(progress != null)
{
progress(total);
}
} while( amtRead == bufferSize );
}
Run Code Online (Sandbox Code Playgroud)
你会这样称呼它:
stream1.CopyToWithProgressAsync(stream2,
4096,
amtCopied => Console.WriteLine(amtCopied))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1032 次 |
| 最近记录: |