Advertisement
Guest User

Untitled

a guest
Aug 29th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. private void btnGetDownload_Click(object sender, EventArgs e)
  2. {
  3. string fileName = System.IO.Path.GetFileName(videosLinks[0]);
  4. DownloadFile(videosLinks[0], @"D:\Videos\videos\" + fileName);
  5. }
  6.  
  7. public void DownloadFile(string urlAddress, string location)
  8. {
  9. Stopwatch sw = new Stopwatch();
  10. using (WebClient webClient = new WebClient())
  11. {
  12. webClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0 Chrome");
  13. webClient.DownloadFileCompleted += new AsyncCompletedEventHandler((sender, e) => Completed(sender, e, sw));
  14. webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => ProgressChanged(sender, e, sw));
  15. Uri URL = new Uri(urlAddress);
  16. sw.Start();
  17. try
  18. {
  19. webClient.DownloadFileAsync(URL, location);
  20. }
  21. catch (Exception ex)
  22. {
  23. MessageBox.Show(ex.Message);
  24. }
  25. }
  26. }
  27.  
  28. private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e, Stopwatch sw)
  29. {
  30. string downloadProgress = e.ProgressPercentage + "%";
  31. string downloadSpeed = string.Format("{0} MB/s", (e.BytesReceived / 1024.0 / 1024.0 / sw.Elapsed.TotalSeconds).ToString("0.00"));
  32. string downloadedMBs = Math.Round(e.BytesReceived / 1024.0 / 1024.0) + " MB";
  33. string totalMBs = Math.Round(e.TotalBytesToReceive / 1024.0 / 1024.0) + " MB";
  34.  
  35. // Format progress string
  36. string progress = $"{downloadedMBs}/{totalMBs} ({downloadProgress}) @ {downloadSpeed}"; // 10 MB / 100 MB (10%) @ 1.23 MB/s
  37.  
  38. progressBarText1.Value = e.ProgressPercentage;
  39. progressBarText1.Refresh();
  40. progressBarText1.CustomText = progress;
  41. }
  42.  
  43. private void Completed(object sender, AsyncCompletedEventArgs e, Stopwatch sw)
  44. {
  45. if (e.Cancelled == true)
  46. {
  47. btnGetDownload.Text = "Fails.";
  48. sw.Stop();
  49. }
  50. else
  51. {
  52. btnGetDownload.Text = "Finish.";
  53. sw.Stop();
  54.  
  55. if (videosLinks.Count > 0)
  56. {
  57. videosLinks.RemoveAt(0);
  58. string fileName = System.IO.Path.GetFileName(videosLinks[0]);
  59. DownloadFile(videosLinks[0], @"D:\Videos\videos\" + fileName);
  60. }
  61. }
  62. }
  63.  
  64. static readonly string[] SizeSuffixes =
  65. { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  66. static string SizeSuffix(Int64 value, Int64 totalValue)
  67. {
  68. if (value < 0) { return "-" + SizeSuffix(-value, totalValue); }
  69.  
  70. int i = 0;
  71. decimal dValue = (decimal)value;
  72. while (Math.Round(dValue / 1024) >= 1)
  73. {
  74. dValue /= 1024;
  75. i++;
  76. }
  77.  
  78. return string.Format("{0:n1} {1} {2}", dValue, SizeSuffixes[i], (totalValue / 1024d / 1024d).ToString("0.00"));
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement