-
Notifications
You must be signed in to change notification settings - Fork 20
/
updatePrompt.cs
103 lines (91 loc) · 3.64 KB
/
updatePrompt.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TurtleWallet
{
public partial class updatePrompt : Form
{
public updatePrompt()
{
InitializeComponent();
}
private void updatePrompt_Load(object sender, EventArgs e)
{
try
{
if (System.AppDomain.CurrentDomain.FriendlyName == "TurtleWallet_update.exe")
{
System.Threading.Thread.Sleep(500);
System.IO.File.Copy("TurtleWallet_update.exe", "TurtleWallet.exe", true);
System.Diagnostics.Process.Start("TurtleWallet.exe");
Environment.Exit(0);
}
else if (System.AppDomain.CurrentDomain.FriendlyName == "TurtleWallet.exe")
{
if (System.IO.File.Exists("TurtleWallet_update.exe"))
System.IO.File.Delete("TurtleWallet_update.exe");
}
}
catch
{
MessageBox.Show("Failed to check for updates!", "TurtleCoin Wallet");
}
updateRequest();
this.Close();
}
void updateRequest()
{
try
{
string thisVersionString = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
bool needsUpdate = false;
var builtURL = "https://api.github.com/repos/turtlecoin/desktop-xamarin/releases/latest";
var cli = new WebClient();
cli.Headers[HttpRequestHeader.ContentType] = "application/json";
cli.Headers[HttpRequestHeader.UserAgent] = "TurtleCoin Wallet " + thisVersionString;
string response = cli.DownloadString(builtURL);
var jobj = JObject.Parse(response);
string gitVersionString = jobj["tag_name"].ToString();
var gitVersion = new Version(gitVersionString);
var thisVersion = new Version(thisVersionString);
var result = gitVersion.CompareTo(thisVersion);
if (result > 0)
needsUpdate = true;
else if (result < 0)
needsUpdate = false;
else
needsUpdate = false;
if (needsUpdate)
{
foreach (var item in jobj["assets"])
{
string name = item["name"].ToString();
if (name.Contains("TurtleWallet.exe"))
{
DialogResult dialogResult = MessageBox.Show("A new version of TurtleCoin Wallet is out. Download?", "TurtleCoin Wallet", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
return;
}
new WebClient().DownloadFile(item["browser_download_url"].ToString(), "TurtleWallet_update.exe");
System.Diagnostics.Process.Start("TurtleWallet_update.exe");
Environment.Exit(0);
}
}
}
}
catch(Exception ex)
{
MessageBox.Show("Failed to check for updates!", "TurtleCoin Wallet");
}
}
}
}