|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Runtime.InteropServices; |
4 | | -//using System.Configuration; |
5 | | -//using System.Data; |
6 | 4 | using System.Linq; |
7 | | -//using System.Threading.Tasks; |
8 | 5 | using System.Windows; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.IO; |
| 8 | +using System.Web.Script.Serialization; |
| 9 | +using System.Net; |
| 10 | +using System.Diagnostics; |
9 | 11 |
|
10 | 12 | namespace DesktopNote |
11 | 13 | { |
@@ -78,6 +80,26 @@ private void RunCheck(object sender1, StartupEventArgs e1) |
78 | 80 | if (!langadded) |
79 | 81 | Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(@"Resources\StringResources.en.xaml", UriKind.Relative) }); |
80 | 82 |
|
| 83 | + // check for updates |
| 84 | + Task.Run(() => { |
| 85 | + var localVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; |
| 86 | + var req = WebRequest.CreateHttp($@"https://api.github.com/repos/changbowen/{nameof(DesktopNote)}/releases/latest"); |
| 87 | + req.ContentType = @"application/json; charset=utf-8"; |
| 88 | + req.UserAgent = nameof(DesktopNote); // needed otherwise 403 |
| 89 | + req.Timeout = 10000; |
| 90 | + using (var res = req.GetResponse()) |
| 91 | + using (var stream = res.GetResponseStream()) |
| 92 | + using (var reader = new StreamReader(stream, System.Text.Encoding.UTF8)) { |
| 93 | + var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(reader.ReadToEnd()); |
| 94 | + if (!dict.TryGetValue("tag_name", out var tagName)) return; |
| 95 | + var remoteVer = Version.Parse(((string)tagName).TrimStart('v')); |
| 96 | + if (localVer < remoteVer && MessageBox.Show(string.Format((string)App.Res["msgbox_new_version_avail"], |
| 97 | + localVer, remoteVer), string.Empty, MessageBoxButton.OKCancel, MessageBoxImage.Information) == MessageBoxResult.OK) { |
| 98 | + Process.Start(@"explorer", $@"https://github.com/changbowen/{nameof(DesktopNote)}/releases"); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + |
81 | 103 | //other run checks |
82 | 104 | if (PathIsNetworkPath(AppDomain.CurrentDomain.BaseDirectory)) { |
83 | 105 | Helpers.MsgBox("msgbox_run_from_network", button: MessageBoxButton.OK, image: MessageBoxImage.Exclamation); |
|
0 commit comments