|
| 1 | +using MetroFramework; |
| 2 | +using System; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Net; |
| 7 | +using System.Threading; |
| 8 | +using System.Windows.Forms; |
| 9 | +using System.Xml; |
| 10 | + |
| 11 | +namespace EzShell |
| 12 | +{ |
| 13 | + public delegate DialogResult ShowMessageDownloade(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon); |
| 14 | + public delegate DialogResult SwMsgDwnldUpt(string message, string header, MessageBoxButtons buttons, MessageBoxIcon icon); |
| 15 | + public class CheckUpdates |
| 16 | + { |
| 17 | + private static string GetRemoteVerLinq = @"/Files/version.xml"; |
| 18 | + private static string GetReleaseNotesLinq = @"/Files/news.xml"; |
| 19 | + public static string AppName { get; set; } |
| 20 | + private static string RemoteVersion { get { return RemoteVer(); } } |
| 21 | + private static string ThisVersion { get; set; } |
| 22 | + private static double RemoteVerDouble { get { return Convert.ToDouble(RemoteVer().Replace(".", "")); } } |
| 23 | + private static double ThisVerDouble { get { return Convert.ToDouble(ThisVersion.Replace(".", "")); } } |
| 24 | + private static SwMsgDwnldUpt swMsg { get; set; } |
| 25 | + public static MetroFramework.Controls.MetroProgressBar progressBar; |
| 26 | + |
| 27 | + public static Label labelProgress; |
| 28 | + private static NotifyIcon notifyIcon; |
| 29 | + public static string UrlLinq { get; set; } |
| 30 | + public CheckUpdates(string _AppName, string _ThisVers, string _UrlLinq, SwMsgDwnldUpt _sw, MetroFramework.Controls.MetroProgressBar _progressBar, NotifyIcon notify, Label labelU) |
| 31 | + { |
| 32 | + progressBar = _progressBar; |
| 33 | + AppName = _AppName; |
| 34 | + ThisVersion = _ThisVers; |
| 35 | + UrlLinq = _UrlLinq; |
| 36 | + swMsg = _sw; |
| 37 | + notifyIcon = notify; |
| 38 | + labelProgress = labelU; |
| 39 | + GetRemoteVerLinq = LinkGenerator() + GetRemoteVerLinq; |
| 40 | + GetReleaseNotesLinq = LinkGenerator() + GetReleaseNotesLinq; |
| 41 | + |
| 42 | + } |
| 43 | + public static string LinkGenerator() |
| 44 | + { |
| 45 | + string linkmaincheck = @"https://fordroid.3dn.ru/pictures/domaincheck.txt"; |
| 46 | + string finaldomain = @""; |
| 47 | + string filename = "linkdomain.txt"; |
| 48 | + // Объект запроса |
| 49 | + HttpWebRequest rew = (HttpWebRequest)WebRequest.Create(linkmaincheck); |
| 50 | + // Отправить запрос и получить ответ |
| 51 | + HttpWebResponse resp = (HttpWebResponse)rew.GetResponse(); |
| 52 | + // Получить поток |
| 53 | + Stream str = resp.GetResponseStream(); |
| 54 | + // Выводим в TextBox |
| 55 | + int ch; |
| 56 | + for (int i = 1; ; i++) |
| 57 | + { |
| 58 | + ch = str.ReadByte(); |
| 59 | + if (ch == -1) break; |
| 60 | + finaldomain += (char)ch; |
| 61 | + } |
| 62 | + // Закрыть поток |
| 63 | + str.Close(); |
| 64 | + return finaldomain; |
| 65 | + } |
| 66 | + public void UpdaterProg(SwMsgDwnldUpt swMs) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + if (ThisVerDouble < RemoteVerDouble) |
| 71 | + { |
| 72 | + |
| 73 | + if (DialogResult.Yes == swMs("New version detected (" + RemoteVersion + ")\nThe application will be automatically updated and restarted\nWhat new:\n" + |
| 74 | + WhatNewVer(swMs), AppName + " v" + ThisVersion, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) |
| 75 | + { |
| 76 | + //panelProgress.Invoke(new Action(() => panelProgress.Visible = true)); |
| 77 | + var ClientDownloader = new WebClient(); |
| 78 | + ClientDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download_ProgressChanged); |
| 79 | + ClientDownloader.DownloadFileCompleted += new AsyncCompletedEventHandler(download_Completed); |
| 80 | + ClientDownloader.DownloadFileAsync(new Uri(@UrlLinq), AppName + ".zip"); |
| 81 | + } |
| 82 | + } |
| 83 | + else if (ThisVerDouble == RemoteVerDouble || ThisVerDouble > RemoteVerDouble) |
| 84 | + swMs("You using the lastest version!\nCurrent version: " + ThisVersion, "No updates available!", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 85 | + } |
| 86 | + catch (Exception exception) |
| 87 | + { |
| 88 | + swMs(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 89 | + } |
| 90 | + } |
| 91 | + private static string WhatNewVer(SwMsgDwnldUpt swMsgDwnld) |
| 92 | + { |
| 93 | + try |
| 94 | + { |
| 95 | + XmlDocument docUpdates = new XmlDocument(); |
| 96 | + docUpdates.Load(GetReleaseNotesLinq); |
| 97 | + return docUpdates.GetElementsByTagName(AppName)[0].InnerText; |
| 98 | + } |
| 99 | + catch (Exception ex) |
| 100 | + { |
| 101 | + //swMsgDwnld(ex.Message, "Can't get release notes", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 102 | + return "\nError.\nCan't load update notes"; |
| 103 | + } |
| 104 | + } |
| 105 | + private static string RemoteVer() |
| 106 | + { |
| 107 | + XmlDocument docVersion = new XmlDocument(); |
| 108 | + try |
| 109 | + { |
| 110 | + docVersion.Load(GetRemoteVerLinq); |
| 111 | + var RemoteVers = docVersion.GetElementsByTagName(AppName)[0].InnerText; |
| 112 | + return RemoteVers; |
| 113 | + } |
| 114 | + catch (Exception) |
| 115 | + { |
| 116 | + return "0"; |
| 117 | + } |
| 118 | + } |
| 119 | + public string MsgUpdateAvailable() |
| 120 | + { |
| 121 | + Thread.Sleep(1500); |
| 122 | + if (ThisVerDouble < RemoteVerDouble) |
| 123 | + { |
| 124 | + return RemoteVersion; |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
| 128 | + private void Notify_BalloonTipClicked(object sender, EventArgs e) |
| 129 | + { |
| 130 | + UpdaterProg(swMsg); |
| 131 | + } |
| 132 | + static string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; |
| 133 | + public static string SizeSuffix(Int64 value) |
| 134 | + { |
| 135 | + if (value < 0) { return "-" + SizeSuffix(-value); } |
| 136 | + int i = 0; |
| 137 | + decimal dValue = (decimal)value; |
| 138 | + while (Math.Round(dValue / 1024) >= 1) |
| 139 | + { |
| 140 | + dValue /= 1024; |
| 141 | + i++; |
| 142 | + } |
| 143 | + return string.Format("{0:n1} {1}", dValue, SizeSuffixes[i]); |
| 144 | + } |
| 145 | + private static void download_ProgressChanged(object sender, DownloadProgressChangedEventArgs e) |
| 146 | + { |
| 147 | + progressBar.Invoke(new Action(() => progressBar.Value = e.ProgressPercentage)); |
| 148 | + labelProgress.Invoke(new Action(() => labelProgress.Text = SizeSuffix(e.BytesReceived) + " / " + SizeSuffix(e.TotalBytesToReceive))); |
| 149 | + } |
| 150 | + private static void download_Completed(object sender, AsyncCompletedEventArgs e) |
| 151 | + { |
| 152 | + // panelProgress.Invoke(new Action(() => panelProgress.Visible = false)); |
| 153 | + try |
| 154 | + { |
| 155 | + if (File.Exists("updater.exe")) |
| 156 | + { |
| 157 | + Process.Start("updater.exe", AppName); |
| 158 | + Thread.Sleep(500); |
| 159 | + Process.GetCurrentProcess().Kill(); |
| 160 | + } |
| 161 | + else |
| 162 | + { |
| 163 | + if (DialogResult.Yes == swMsg("Updater.exe not founded! Download the updater?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)) |
| 164 | + { |
| 165 | + var ClientDownloader = new WebClient(); |
| 166 | + ClientDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(download_ProgressChanged); |
| 167 | + ClientDownloader.DownloadFileCompleted += new AsyncCompletedEventHandler(download_Completed); |
| 168 | + ClientDownloader.DownloadFileAsync(new Uri(LinkGenerator()+ @"/Files/updater.exe"), "updater.exe"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + catch (Win32Exception ex) |
| 174 | + { |
| 175 | + swMsg("Updater.exe not founded! " + |
| 176 | + "Please download the latest version of this program from droidapps.cf\n" + ex.Message, "Error", |
| 177 | + MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments