|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Windows.Forms; |
| 4 | +using System.Net; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Drawing; |
| 7 | +using System.IO; |
| 8 | +using Microsoft.Win32; |
| 9 | + |
| 10 | +namespace QuickLibrary |
| 11 | +{ |
| 12 | + public partial class DownloadForm : Form |
| 13 | + { |
| 14 | + private string fileName; |
| 15 | + private WebClient wc; |
| 16 | + private string url; |
| 17 | + |
| 18 | + public DownloadForm(string url, bool darkMode) |
| 19 | + { |
| 20 | + if (darkMode) |
| 21 | + { |
| 22 | + this.HandleCreated += new EventHandler(ThemeManager.formHandleCreated); |
| 23 | + } |
| 24 | + |
| 25 | + this.url = url; |
| 26 | + fileName = Path.Combine(GetDownloadFolderPath(), System.IO.Path.GetFileName(url)); |
| 27 | + |
| 28 | + InitializeComponent(); |
| 29 | + |
| 30 | + if (darkMode) |
| 31 | + { |
| 32 | + this.BackColor = ThemeManager.DarkBackColor; |
| 33 | + this.ForeColor = Color.White; |
| 34 | + |
| 35 | + cancelButton.BackColor = ThemeManager.DarkSecondColor; |
| 36 | + updateButton.BackColor = ThemeManager.DarkSecondColor; |
| 37 | + |
| 38 | + manuallyLink.LinkColor = ThemeManager.AccentColor; |
| 39 | + } |
| 40 | + |
| 41 | + wc = new WebClient(); |
| 42 | + |
| 43 | + wc.DownloadProgressChanged += wc_DownloadProgressChanged; |
| 44 | + wc.DownloadFileCompleted += wc_DownloadFileCompleted; |
| 45 | + |
| 46 | + wc.DownloadFileAsync(new Uri(url), fileName); |
| 47 | + } |
| 48 | + |
| 49 | + private string GetDownloadFolderPath() |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + return Registry.GetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", "{374DE290-123F-4565-9164-39C4925E467B}", String.Empty).ToString(); |
| 54 | + } |
| 55 | + catch |
| 56 | + { |
| 57 | + return ""; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) |
| 62 | + { |
| 63 | + progressBar1.Value = e.ProgressPercentage; |
| 64 | + statusLabel.Text = string.Format("Downloading... {0}% ({1} / {2})", e.ProgressPercentage, BytesToSize(e.BytesReceived), BytesToSize(e.TotalBytesToReceive)); |
| 65 | + } |
| 66 | + |
| 67 | + private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) |
| 68 | + { |
| 69 | + if (e.Error != null) |
| 70 | + { |
| 71 | + statusLabel.Text = "Update failed!"; |
| 72 | + } |
| 73 | + else if (e.Cancelled) |
| 74 | + { |
| 75 | + statusLabel.Text = "Update cancelled!"; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + updateButton.Visible = true; |
| 80 | + statusLabel.Text = "Download finished!"; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private void updateButton_Click(object sender, EventArgs e) |
| 85 | + { |
| 86 | + Process.Start(fileName); |
| 87 | + this.Close(); |
| 88 | + this.Owner.Close(); |
| 89 | + } |
| 90 | + |
| 91 | + private void cancelButton_Click(object sender, EventArgs e) |
| 92 | + { |
| 93 | + wc.CancelAsync(); |
| 94 | + } |
| 95 | + |
| 96 | + private void manuallyLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) |
| 97 | + { |
| 98 | + wc.CancelAsync(); |
| 99 | + Process.Start(url); |
| 100 | + this.Close(); |
| 101 | + } |
| 102 | + |
| 103 | + private void DownloadForm_FormClosing(object sender, FormClosingEventArgs e) |
| 104 | + { |
| 105 | + wc.CancelAsync(); |
| 106 | + } |
| 107 | + |
| 108 | + private string BytesToSize(double len) |
| 109 | + { |
| 110 | + string[] sizes = { "B", "KB", "MB", "GB", "TB" }; |
| 111 | + int order = 0; |
| 112 | + while (len >= 1024 && order < sizes.Length - 1) |
| 113 | + { |
| 114 | + order++; |
| 115 | + len = len / 1024; |
| 116 | + } |
| 117 | + |
| 118 | + return String.Format("{0:0.##} {1}", len, sizes[order]); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments