icorrelate

DownloaderApp.cs

Dec 13th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Windows.Forms;
  7.  
  8. namespace DownloaderApp
  9. {
  10. public partial class Form1 : Form
  11. {
  12. public Form1()
  13. {
  14. InitializeComponent();
  15. }
  16.  
  17. private void btndownload_Click(object sender, EventArgs e)
  18. {
  19. string filename = getFileName(txturl.Text);
  20. string url = txturl.Text;
  21.  
  22. lblfilename.Text = "File Name: " + getFileName(url);
  23. using (WebClient wc = new WebClient())
  24. {
  25. wc.DownloadFileCompleted += wc_completed;
  26. wc.DownloadProgressChanged += wc_DownloadProgressChanged;
  27. wc.DownloadFileAsync(new Uri(url), txtpath.Text + filename);
  28. }
  29. }
  30.  
  31. private void wc_completed(object sender, AsyncCompletedEventArgs e)
  32. {
  33. MessageBox.Show("Download Completed!");
  34. }
  35.  
  36. void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  37. {
  38. progressBar.Value = e.ProgressPercentage;
  39. txtpercent.Text = e.ProgressPercentage + "%";
  40.  
  41. double bytesIn = double.Parse(e.BytesReceived.ToString());
  42. double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
  43. lbldownloaded.Text = "Downloaded: " + bytesIn + "/" + totalBytes;
  44. }
  45.  
  46. private void btnbrowse_Click(object sender, EventArgs e)
  47. {
  48. using (FolderBrowserDialog dlg = new FolderBrowserDialog())
  49. {
  50. dlg.Description = "Select a folder";
  51. if (dlg.ShowDialog() == DialogResult.OK)
  52. {
  53. txtpath.Text = dlg.SelectedPath + "\\";
  54. }
  55. }
  56. }
  57.  
  58. private String getFileSize(String url)
  59. {
  60. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  61. req.Method = "HEAD";
  62. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  63. long len = resp.ContentLength;
  64.  
  65. return len.ToString();
  66. }
  67.  
  68. //Source: http://stackoverflow.com/questions/13570839/get-filename-without-content-disposition
  69. private String getFileName(string url)
  70. {
  71. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  72. string fileName = "";
  73. try
  74. {
  75.  
  76. HttpWebResponse res = (HttpWebResponse)request.GetResponse();
  77. using (Stream rstream = res.GetResponseStream())
  78. {
  79. fileName = res.Headers["Content-Disposition"] != null ?
  80. res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace("\"", "") :
  81. res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) :
  82. Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ?
  83. Path.GetFileName(res.ResponseUri.ToString()) : GetFileName(url);
  84. }
  85. res.Close();
  86. }
  87. catch {}
  88.  
  89. return fileName;
  90. }
  91.  
  92.  
  93. private string GetFileName(string url)
  94. {
  95. string[] parts = url.Split('/');
  96. string fileName = "";
  97.  
  98. if (parts.Length > 0)
  99. fileName = parts[parts.Length - 1];
  100. else
  101. fileName = url;
  102. return fileName;
  103. }
  104. }
  105. }
Add Comment
Please, Sign In to add comment