Skip to content

Instantly share code, notes, and snippets.

@bassuny3003
Created April 6, 2023 12:36
Show Gist options
  • Save bassuny3003/2c1cef498ee49a99460bc5e55c3871a2 to your computer and use it in GitHub Desktop.
Save bassuny3003/2c1cef498ee49a99460bc5e55c3871a2 to your computer and use it in GitHub Desktop.
Download File Using C#
private void DownloadFile(string urlAddress, string location)
{
using (WebClient client = new WebClient())
{
Uri uri = new Uri(urlAddress);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
client.DownloadFileAsync(uri, location);
}
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
DownloadProgbar.Value = e.ProgressPercentage;
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled == true)
{
MessageBox.Show("Download has been canceled.");
}
else
{
MessageBox.Show("Download completed!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment