Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- namespace DownloadManager
- {
- public partial class Form1 : MaterialSkin.Controls.MaterialForm
- {
- List<ToDownload> listOfToDownloadItems = new List<ToDownload>();
- BindingSource source = new BindingSource();
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- source.DataSource = listOfToDownloadItems;
- ToDownloadGrid.DataSource = source;
- }
- private void pathButton_Click(object sender, EventArgs e)
- {
- var folderBrowserDialog1 = new FolderBrowserDialog();
- DialogResult result = folderBrowserDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- var fileSavePath = folderBrowserDialog1.SelectedPath;
- }
- }
- private void downloadButton_Click(object sender, EventArgs e)
- {
- string fileName = nameBox.Text;
- string urlName = urlBox.Text;
- string fileNameEnd;
- if (urlName.Length == 0)
- {
- MessageBox.Show("You haven't set the url of the object to download!");
- return;
- }
- if (fileName.Length == 0)
- {
- fileNameEnd = System.IO.Path.GetFileName(urlName);
- }
- else
- {
- fileNameEnd = fileName;
- }
- var extension = System.IO.Path.GetExtension(urlName);
- string myLocalFilePath = @"C:\Users\Tim\Documents\pics\" + fileNameEnd + extension;
- using (var client = new WebClient())
- {
- inProgress.Text = "Downloading...";
- string[] split = urlName.Split('/');
- string NameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(split[split.Length - 1]);
- ToDownload item = new ToDownload();
- item.ID = listOfToDownloadItems.Count;
- item.Name = NameWithoutExt;
- item.Status = "Downloading";
- client.OpenRead(urlName);
- Int64 bytes_total = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
- item.Size = SizeSuffix(bytes_total);
- item.Added = DateTime.Now;
- listOfToDownloadItems.Add(item);
- source.ResetBindings(false);
- client.DownloadProgressChanged += (sender2, e2) => Client_DownloadProgressChanged(sender2, e2, item.ID)
- client.DownloadFileCompleted += (sender3, e3) => Client_DownloadFileCompleted(sender3, e3, item.ID);
- client.DownloadFileAsync(new Uri(urlName), myLocalFilePath);
- }
- }
- private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e, int ID)
- {
- //progress bar
- listOfToDownloadItems[ID].Status = "Downloading %" + e.ProgressPercentage;
- source.ResetBindings(false);
- }
- private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e, int ID)
- {
- //download complete
- listOfToDownloadItems[ID].Status = "Download Finished";
- source.ResetBindings(false);
- }
- static readonly string[] SizeSuffixes =
- { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
- static string SizeSuffix(Int64 value, int decimalPlaces = 1)
- {
- if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
- if (value < 0) { return "-" + SizeSuffix(-value); }
- if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
- // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
- int mag = (int)Math.Log(value, 1024);
- // 1L << (mag * 10) == 2 ^ (10 * mag)
- // [i.e. the number of bytes in the unit corresponding to mag]
- decimal adjustedSize = (decimal)value / (1L << (mag * 10));
- // make adjustment when the value is large enough that
- // it would round up to 1000 or more
- if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
- {
- mag += 1;
- adjustedSize /= 1024;
- }
- return string.Format("{0:n" + decimalPlaces + "} {1}",
- adjustedSize,
- SizeSuffixes[mag]);
- }
- private void urlBox_TextChanged(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement