Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.Net;
  12.  
  13. namespace DownloadManager
  14. {
  15.     public partial class Form1 : MaterialSkin.Controls.MaterialForm
  16.     {
  17.         List<ToDownload> listOfToDownloadItems = new List<ToDownload>();
  18.         BindingSource source = new BindingSource();
  19.  
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.  
  28.             source.DataSource = listOfToDownloadItems;
  29.             ToDownloadGrid.DataSource = source;
  30.  
  31.         }
  32.  
  33.         private void pathButton_Click(object sender, EventArgs e)
  34.         {
  35.             var folderBrowserDialog1 = new FolderBrowserDialog();
  36.             DialogResult result = folderBrowserDialog1.ShowDialog();
  37.             if (result == DialogResult.OK)
  38.             {
  39.                 var fileSavePath = folderBrowserDialog1.SelectedPath;
  40.             }
  41.         }
  42.  
  43.         private void downloadButton_Click(object sender, EventArgs e)
  44.         {
  45.             string fileName = nameBox.Text;
  46.             string urlName = urlBox.Text;
  47.             string fileNameEnd;
  48.  
  49.             if (urlName.Length == 0)
  50.             {
  51.                 MessageBox.Show("You haven't set the url of the object to download!");
  52.                 return;
  53.             }
  54.             if (fileName.Length == 0)
  55.             {
  56.                 fileNameEnd = System.IO.Path.GetFileName(urlName);
  57.             }
  58.             else
  59.             {
  60.                 fileNameEnd = fileName;
  61.             }
  62.  
  63.  
  64.  
  65.             var extension = System.IO.Path.GetExtension(urlName);
  66.             string myLocalFilePath = @"C:\Users\Tim\Documents\pics\" + fileNameEnd + extension;
  67.  
  68.  
  69.  
  70.             using (var client = new WebClient())
  71.             {
  72.                 inProgress.Text = "Downloading...";
  73.  
  74.                 string[] split = urlName.Split('/');
  75.                 string NameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(split[split.Length - 1]);
  76.  
  77.                 ToDownload item = new ToDownload();
  78.                 item.ID = listOfToDownloadItems.Count;
  79.                 item.Name = NameWithoutExt;
  80.                 item.Status = "Downloading";
  81.  
  82.                 client.OpenRead(urlName);
  83.                 Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
  84.  
  85.  
  86.                 item.Size = SizeSuffix(bytes_total);
  87.                 item.Added = DateTime.Now;
  88.  
  89.                 listOfToDownloadItems.Add(item);
  90.  
  91.                 source.ResetBindings(false);
  92.                 client.DownloadProgressChanged += (sender2, e2) => Client_DownloadProgressChanged(sender2, e2, item.ID)
  93.                 client.DownloadFileCompleted += (sender3, e3) => Client_DownloadFileCompleted(sender3, e3, item.ID);
  94.                 client.DownloadFileAsync(new Uri(urlName), myLocalFilePath);
  95.  
  96.  
  97.             }
  98.  
  99.         }
  100.  
  101.         private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e, int ID)
  102.         {
  103.             //progress bar
  104.             listOfToDownloadItems[ID].Status = "Downloading %" + e.ProgressPercentage;
  105.                 source.ResetBindings(false);
  106.  
  107.            
  108.         }
  109.  
  110.         private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e, int ID)
  111.         {
  112.             //download complete
  113.  
  114.             listOfToDownloadItems[ID].Status = "Download Finished";
  115.                 source.ResetBindings(false);
  116.  
  117.         }
  118.  
  119.  
  120.         static readonly string[] SizeSuffixes =
  121.                    { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  122.  
  123.         static string SizeSuffix(Int64 value, int decimalPlaces = 1)
  124.         {
  125.             if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
  126.             if (value < 0) { return "-" + SizeSuffix(-value); }
  127.             if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
  128.  
  129.             // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
  130.             int mag = (int)Math.Log(value, 1024);
  131.  
  132.             // 1L << (mag * 10) == 2 ^ (10 * mag)
  133.             // [i.e. the number of bytes in the unit corresponding to mag]
  134.             decimal adjustedSize = (decimal)value / (1L << (mag * 10));
  135.  
  136.             // make adjustment when the value is large enough that
  137.             // it would round up to 1000 or more
  138.             if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
  139.             {
  140.                 mag += 1;
  141.                 adjustedSize /= 1024;
  142.             }
  143.  
  144.             return string.Format("{0:n" + decimalPlaces + "} {1}",
  145.                 adjustedSize,
  146.                 SizeSuffixes[mag]);
  147.         }
  148.  
  149.         private void urlBox_TextChanged(object sender, EventArgs e)
  150.         {
  151.  
  152.         }
  153.  
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement