Falcon
Yönetici
Kod:
async Task DownloadWithProgress(string url, string path, ProgressBar bar)
{
using HttpClient client = new HttpClient();
using HttpResponseMessage response =
await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
long total = response.Content.Headers.ContentLength ?? 0;
using var stream = await response.Content.ReadAsStreamAsync();
using var fs = new FileStream(path, FileMode.Create, FileAccess.Write);
byte[] buffer = new byte[8192];
long readTotal = 0;
int read;
while ((read = await stream.ReadAsync(buffer)) > 0)
{
await fs.WriteAsync(buffer.AsMemory(0, read));
readTotal += read;
bar.Value = (int)(readTotal * 100 / total);
Application.DoEvents();
}
}