-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdaterForm.cs
157 lines (148 loc) · 5.98 KB
/
UpdaterForm.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.IO.Compression;
namespace SRVTracker
{
public partial class UpdaterForm : Form
{
private VersionInfo _updateInformation = null;
private Updater _updater = null;
private bool _errorPreventsClose = false;
public UpdaterForm()
{
InitializeComponent();
textBoxThisVersion.Text = Application.ProductVersion;
}
public UpdaterForm(VersionInfo updateInformation): this()
{
// This is the entry point to prompt whether to update or not - so we hide the updating group box so that the update options can be seen
_updateInformation = updateInformation;
textBoxAvailableVersion.Text = _updateInformation.version;
groupBoxUpdating.Visible = false;
}
public UpdaterForm(Updater updater): this()
{
// This is the entry point if we are updating. The .update file should contain the update information
if (!File.Exists(updater.ApplicationUpdateInfoFile()))
MessageBox.Show("Update information file not found", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
try
{
string updateInfo = File.ReadAllText(updater.ApplicationUpdateInfoFile());
_updateInformation = VersionInfo.FromString(updateInfo);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to get latest version{Environment.NewLine}{Environment.NewLine}{ex.Message}", "Update Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if (_updateInformation == null)
{
groupBoxUpdating.Visible = false;
buttonYes.Enabled = false;
buttonNo.Text = "Close";
return;
}
_updater = updater;
this.Width = 800;
groupBoxUpdating.Visible = true;
Action action = new Action(() =>
{
InstallUpdate();
if (!_errorPreventsClose) // Close is prevented if a potentially fatal error occurred during update
Close();
});
Task.Run(action);
}
private void DownloadAndExtractZip()
{
// Download the latest zip from the update Url, and extract the contents
try
{
AddLog($"Downloading update from {_updateInformation.downloadUrl}");
WebRequest wrq = WebRequest.Create(_updateInformation.downloadUrl);
WebResponse wrs = wrq.GetResponse();
AddLog("Update retrieved, unpacking");
using (Stream response = wrs.GetResponseStream())
{
ZipArchive zip = new ZipArchive(response);
foreach (var entry in zip.Entries)
{
try
{
AddLog($"Deleting {entry.FullName}");
File.Delete(entry.FullName);
}
catch (Exception ex)
{
AddLog($"Error: {ex.Message}");
}
var d = Path.GetDirectoryName(entry.FullName);
if (!string.IsNullOrEmpty(d))
{
try
{
AddLog($"Creating directory {d}");
Directory.CreateDirectory(d);
}
catch (Exception ex)
{
AddLog($"Error: {ex.Message}");
}
}
AddLog($"Writing {entry.FullName}");
try
{
using (Stream zipStream = entry.Open())
using (FileStream fileStream = File.OpenWrite(entry.FullName))
zipStream.CopyTo(fileStream);
}
catch (Exception ex)
{
AddLog($"Error: {ex.Message}");
_errorPreventsClose = true;
}
}
}
}
catch (Exception ex)
{
AddLog($"Error: {ex.Message}");
_errorPreventsClose = true;
}
}
private void AddLog(string info)
{
Action action = new Action(() =>
{
if (String.IsNullOrEmpty(textBoxUpdateProgress.Text))
textBoxUpdateProgress.Text = info;
else
textBoxUpdateProgress.Text = $"{textBoxUpdateProgress.Text}{Environment.NewLine}{info}";
textBoxUpdateProgress.SelectionStart = textBoxUpdateProgress.TextLength-1;
textBoxUpdateProgress.ScrollToCaret();
});
if (textBoxUpdateProgress.InvokeRequired)
textBoxUpdateProgress.Invoke(action);
else
action();
}
private void InstallUpdate()
{
AddLog($"Updating to version {_updateInformation.version}");
DownloadAndExtractZip();
AddLog($"Update complete. Restarting application.");
string appPath = $"{Application.ExecutablePath.Substring(0, Application.ExecutablePath.Length - 11)}.exe";
Updater.LaunchApplication(appPath);
}
private void buttonNo_MouseClick(object sender, MouseEventArgs e)
{
if (!this.Modal)
Close();
}
}
}