Skip to content

Commit

Permalink
增加了启动时检查新版本的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
LanbingIce committed Sep 19, 2023
1 parent c3ba08c commit 4ec514f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions IsaacSocket/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using IsaacSocket.Utils;
using System.Diagnostics;
using System.Reflection;
using System.Text.Json;

namespace IsaacSocket;

Expand All @@ -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);
}
}
}

0 comments on commit 4ec514f

Please sign in to comment.