This is working fine :
private void btnGetDownload_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(videosLinks[0]);
DownloadFile(videosLinks[0], @"D:\Videos\videos\" + fileName);
}
public void DownloadFile(string urlAddress, string location)
{
Stopwatch sw = new Stopwatch();
using (WebClient webClient = new WebClient())
{
webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0 Chrome");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler((sender, e) => Completed(sender, e, sw));
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => ProgressChanged(sender, e, sw));
Uri URL = new Uri(urlAddress);
sw.Start();
try
{
webClient.DownloadFileAsync(URL, location);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e, Stopwatch sw)
{
progressBar1.Value = e.ProgressPercentage;
lblFileName.Text = string.Format("data", string.Format("{0} / {1} MB",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00")));
btnGetDownload.Text = "Downloading...";
label1.Text = string.Format("{0} Kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
}
private void Completed(object sender, AsyncCompletedEventArgs e, Stopwatch sw)
{
if (e.Cancelled == true)
{
btnGetDownload.Text = "Fails.";
sw.Stop();
}
else
{
btnGetDownload.Text = "Finish.";
label1.Text = "0 Kb/s";
sw.Stop();
if (videosLinks.Count > 0)
{
videosLinks.RemoveAt(0);
string fileName = System.IO.Path.GetFileName(videosLinks[0]);
DownloadFile(videosLinks[0], @"D:\Videos\videos\" + fileName);
}
}
}