Скачивание файла с интернета (progressbar and download speed)




using System.Diagnostics;
using System.IO;
using System.Net;

WebClient webClient;    // Our WebClient that will be doing the downloading for us
Stopwatch sw = new Stopwatch();    // The stopwatch which we will be using to calculate the download speed

public void downloadFile(string urlAddress, string location)
{
   using (webClient = new WebClient())
   {
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
      try
      {
         // The variable that will be holding the url address
         Uri URL;
         // Make sure the url starts with "http://"
         if (!urlAddress.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
            URL = new Uri("http://" + urlAddress);
         else
            URL = new Uri(urlAddress);
         // Start the stopwatch which we will be using to calculate the download speed
         sw.Start();
         // Start downloading the file
         webClient.DownloadFileAsync(URL, location);
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
}
 
// The event that will fire whenever the progress of the WebClient is changed
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
   try
   {
      // Calculate download speed and output it to label3
      if (labelPerc.Text != (Convert.ToDouble(e.BytesReceived) / 1024 / sw.Elapsed.TotalSeconds).ToString("0"))
         labelSpeed.Text = (Convert.ToDouble(e.BytesReceived) / 1024 / sw.Elapsed.TotalSeconds).ToString("0.00") + " kb/s";
 
      // Update the progressbar percentage only when the value is not the same (to avoid updating the control constantly)
      if (progressBar.Value != e.ProgressPercentage)
         progressBar.Value = e.ProgressPercentage;
 
      // Show the percentage on our label (update only if the value isn't the same to avoid updating the control constantly)
      if (labelPerc.Text != e.ProgressPercentage.ToString() + "%")
         labelPerc.Text = e.ProgressPercentage.ToString() + "%";
 
      // Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
      labelDownloaded.Text = (Convert.ToDouble(e.BytesReceived) / 1024 / 1024).ToString("0.00") + " Mb's" + "  /  " + (Convert.ToDouble(e.TotalBytesToReceive) / 1024 / 1024).ToString("0.00") + " Mb's";
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}
 
// The event that will trigger when the WebClient is completed
private void Completed(object sender, AsyncCompletedEventArgs e)
{
   sw.Reset();
   if (e.Cancelled == true)
   {
      File.Delete(saveFileDialog.FileName);       // Delete the incomplete file if the download is canceled
      MessageBox.Show("Canceled");
   }
   else
      MessageBox.Show("Download completed!");
}
Взято с www.fluxbytes.com

Комментариев нет:

Отправить комментарий

Большая просьба, не писать в комментариях всякую ерунду не по теме!