From 4ec514f774eda0dd68bb14f7b442e9384a96d6e6 Mon Sep 17 00:00:00 2001 From: LanbingIce <895446745@qq.com> Date: Wed, 20 Sep 2023 06:30:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E6=A3=80=E6=9F=A5=E6=96=B0=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IsaacSocket/Program.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/IsaacSocket/Program.cs b/IsaacSocket/Program.cs index 6c02cd1..4c11c8a 100644 --- a/IsaacSocket/Program.cs +++ b/IsaacSocket/Program.cs @@ -1,5 +1,7 @@ using IsaacSocket.Utils; using System.Diagnostics; +using System.Reflection; +using System.Text.Json; namespace IsaacSocket; @@ -21,8 +23,45 @@ static void Main() } // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. + _ = CheckUpdate(); ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } + + static async Task CheckUpdate() + { + using HttpClient client = new(); + client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31"); + try + { + string json = await client.GetStringAsync("https://api.github.com/repos/LanbingIce/IsaacSocket-Utility/releases/latest"); + using JsonDocument doc = JsonDocument.Parse(json); + JsonElement root = doc.RootElement; + + string tagName = root.GetProperty("tag_name").GetString() ?? "v1.0"; + string versionString = tagName[1..]; + string[] parts = versionString.Split('.'); + for (int i = 0; i < 4 - parts.Length; i++) + { + versionString += ".0"; + } + + Version? clientVersion = Assembly.GetEntryAssembly()?.GetName().Version; + Version serverVersion = new(versionString); + + if (serverVersion.CompareTo(clientVersion) > 0) + { + DialogResult result = MessageBox.Show($"{tagName} 更新说明:{Environment.NewLine}{Environment.NewLine}{root.GetProperty("body").GetString() ?? ""}{Environment.NewLine}{Environment.NewLine}是否要现在下载?", $"软件有更新版本:{ tagName}", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (result == DialogResult.Yes) + { + Process.Start("explorer.exe", root.GetProperty("assets")[0].GetProperty("browser_download_url").GetString() ?? "https://github.com/LanbingIce/IsaacSocket-Utility/releases/latest"); + } + } + } + catch (Exception e) + { + await Console.Out.WriteLineAsync(e.Message); + } + } }