[C#] How to download file from web
using System.Net;
...
// [Download File Synchronously]
private void StartDownload(string sURLFileName, string sLocalTargetFilePathName)
{
WebClient webClient = new WebClient();
webClient.DownloadFile(sURLFileName, sLocalTargetFilePathName);
}
// [Download File Asynchronously]
private void StartDownloadAsync(string sURLFileName, string sLocalTargetFilePathName)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDonwloadCompleted);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgress);
webClient.DownloadFileAsync(new Uri(sURLFileName), sLocalTargetFilePathName);
}
private void OnDownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void OnDonwloadCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}