1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| private async Task<long> CopyFile(string source, string destination)
{
using (var sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read))
using (var destinationStream = new FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write))
{
progressBar1.Minimum = 0;
progressBar1.Maximum = (int)sourceStream.Length;
progressBar1.Value = 0;
long totalBytesCopied = 0;
int nRead;
byte[] buffer = new byte[8192];
while ((nRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destinationStream.WriteAsync(buffer, 0, nRead);
totalBytesCopied += nRead;
progressBar1.Value = (int)totalBytesCopied;
}
return totalBytesCopied;
}
} |