-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateManagerForm.cs
355 lines (334 loc) · 15.8 KB
/
UpdateManagerForm.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
using UpdateManager.Languages;
namespace UpdateManager
{
public class UpdateManagerForm : Form
{
private Button btnInstallUpdates;
private Label lblCurrentVersion;
private Label lblNewVersion;
private Label lblANewVersionAva;
private ProgressBar progressBar;
private Label lblStatus;
private Button btnDownloadUpdates;
private LinkLabel lnkLblDirectDownload;
private Button btnCheckUpdates;
private LinkLabel lnkLblOpenFolder;
private readonly string productName = Application.ProductName;
private readonly string productVersion = Application.ProductVersion;
private readonly bool is64BitProcess = Environment.Is64BitProcess;
private string UrlGitHubReleases;
private string urlDownload;
private string UrlVersion;
private string UrlSHA256;
private string path;
private double FileSize;
private double Percentage;
public UpdateManagerForm(string appName, string urlVersion, string urlGitHubReleases, string urlSHA256)
{
InitializeComponent();
lblStatus.Text = "";
UrlVersion = urlVersion;
UrlGitHubReleases = urlGitHubReleases;
UrlSHA256 = urlSHA256;
if (appName != null && appName != "")
{
productName = appName;
}
Lang();
}
private void InitializeComponent()
{
this.btnInstallUpdates = new System.Windows.Forms.Button();
this.lblCurrentVersion = new System.Windows.Forms.Label();
this.lblNewVersion = new System.Windows.Forms.Label();
this.lblANewVersionAva = new System.Windows.Forms.Label();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.lblStatus = new System.Windows.Forms.Label();
this.btnDownloadUpdates = new System.Windows.Forms.Button();
this.lnkLblDirectDownload = new System.Windows.Forms.LinkLabel();
this.btnCheckUpdates = new System.Windows.Forms.Button();
this.lnkLblOpenFolder = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// btnInstallUpdates
//
this.btnInstallUpdates.AutoSize = true;
this.btnInstallUpdates.Enabled = false;
this.btnInstallUpdates.Location = new System.Drawing.Point(12, 154);
this.btnInstallUpdates.Name = "btnInstallUpdates";
this.btnInstallUpdates.Size = new System.Drawing.Size(132, 23);
this.btnInstallUpdates.TabIndex = 2;
this.btnInstallUpdates.Text = "&Install Updates";
this.btnInstallUpdates.UseVisualStyleBackColor = true;
this.btnInstallUpdates.Click += new System.EventHandler(this.btnInstallUpdates_Click);
//
// lblCurrentVersion
//
this.lblCurrentVersion.AutoSize = true;
this.lblCurrentVersion.Location = new System.Drawing.Point(12, 46);
this.lblCurrentVersion.Name = "lblCurrentVersion";
this.lblCurrentVersion.Size = new System.Drawing.Size(82, 13);
this.lblCurrentVersion.TabIndex = 1;
this.lblCurrentVersion.Text = "Current Version:";
//
// lblNewVersion
//
this.lblNewVersion.AutoSize = true;
this.lblNewVersion.Location = new System.Drawing.Point(12, 59);
this.lblNewVersion.Name = "lblNewVersion";
this.lblNewVersion.Size = new System.Drawing.Size(68, 13);
this.lblNewVersion.TabIndex = 1;
this.lblNewVersion.Text = "Last Version:";
//
// lblANewVersionAva
//
this.lblANewVersionAva.AutoSize = true;
this.lblANewVersionAva.Location = new System.Drawing.Point(12, 9);
this.lblANewVersionAva.Name = "lblANewVersionAva";
this.lblANewVersionAva.Size = new System.Drawing.Size(136, 13);
this.lblANewVersionAva.TabIndex = 2;
this.lblANewVersionAva.Text = "A New Version is Available.";
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
this.progressBar.Location = new System.Drawing.Point(12, 197);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(251, 23);
this.progressBar.TabIndex = 3;
//
// lblStatus
//
this.lblStatus.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.lblStatus.AutoSize = true;
this.lblStatus.Location = new System.Drawing.Point(8, 227);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(37, 13);
this.lblStatus.TabIndex = 4;
this.lblStatus.Text = "Status";
//
// btnDownloadUpdates
//
this.btnDownloadUpdates.AutoSize = true;
this.btnDownloadUpdates.Enabled = false;
this.btnDownloadUpdates.Location = new System.Drawing.Point(12, 120);
this.btnDownloadUpdates.Name = "btnDownloadUpdates";
this.btnDownloadUpdates.Size = new System.Drawing.Size(132, 23);
this.btnDownloadUpdates.TabIndex = 1;
this.btnDownloadUpdates.Text = "&Download Updates";
this.btnDownloadUpdates.UseVisualStyleBackColor = true;
this.btnDownloadUpdates.Click += new System.EventHandler(this.btnDownloadUpdates_Click);
//
// lnkLblDirectDownload
//
this.lnkLblDirectDownload.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lnkLblDirectDownload.AutoSize = true;
this.lnkLblDirectDownload.Enabled = false;
this.lnkLblDirectDownload.Location = new System.Drawing.Point(175, 130);
this.lnkLblDirectDownload.Name = "lnkLblDirectDownload";
this.lnkLblDirectDownload.Size = new System.Drawing.Size(86, 13);
this.lnkLblDirectDownload.TabIndex = 1;
this.lnkLblDirectDownload.TabStop = true;
this.lnkLblDirectDownload.Text = "&Direct Download";
this.lnkLblDirectDownload.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkLblDirectDownload_LinkClicked);
//
// btnCheckUpdates
//
this.btnCheckUpdates.AutoSize = true;
this.btnCheckUpdates.Location = new System.Drawing.Point(11, 91);
this.btnCheckUpdates.Name = "btnCheckUpdates";
this.btnCheckUpdates.Size = new System.Drawing.Size(135, 23);
this.btnCheckUpdates.TabIndex = 0;
this.btnCheckUpdates.Text = "&Check For Updates";
this.btnCheckUpdates.UseVisualStyleBackColor = true;
this.btnCheckUpdates.Click += new System.EventHandler(this.btnCheckUpdates_Click);
//
// lnkLblOpenFolder
//
this.lnkLblOpenFolder.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lnkLblOpenFolder.AutoSize = true;
this.lnkLblOpenFolder.Location = new System.Drawing.Point(8, 249);
this.lnkLblOpenFolder.Name = "lnkLblOpenFolder";
this.lnkLblOpenFolder.Size = new System.Drawing.Size(74, 13);
this.lnkLblOpenFolder.TabIndex = 3;
this.lnkLblOpenFolder.TabStop = true;
this.lnkLblOpenFolder.Text = "&Show in folder";
this.lnkLblOpenFolder.Visible = false;
this.lnkLblOpenFolder.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.lnkLblOpenFolder_LinkClicked);
//
// UpdateManagerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.ClientSize = new System.Drawing.Size(273, 274);
this.Controls.Add(this.lnkLblOpenFolder);
this.Controls.Add(this.lnkLblDirectDownload);
this.Controls.Add(this.btnCheckUpdates);
this.Controls.Add(this.btnDownloadUpdates);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.lblANewVersionAva);
this.Controls.Add(this.lblNewVersion);
this.Controls.Add(this.lblCurrentVersion);
this.Controls.Add(this.btnInstallUpdates);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "UpdateManagerForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Update Manager";
this.Load += new System.EventHandler(this.UpdateManagerForm_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void Lang()
{
this.Text = Localization.UpdateManager;
lblNewVersion.Text = "";
lblANewVersionAva.Text = "";
btnCheckUpdates.Text = Localization.CheckUpdates;
btnInstallUpdates.Text = Localization.InstallUpdates;
btnDownloadUpdates.Text = Localization.DownloadUpdates;
lnkLblDirectDownload.Text = Localization.DirectDownload;
lblCurrentVersion.Text = Localization.CurrentVersion + " " + productVersion + "\r\n";
lnkLblOpenFolder.Text = Localization.OpenFolder;
}
private void CheckUpdate()
{
try
{
btnCheckUpdates.Enabled = false;
lnkLblDirectDownload.Enabled = false;
bool ass = UpdateManager.Update.Check(productVersion, is64BitProcess, UrlVersion, UrlGitHubReleases + "download/", productName + ".Setup", Environment.GetEnvironmentVariable("USERPROFILE") + @"\Downloads", out string newVersion, out urlDownload, out path);
if (ass)
{
btnDownloadUpdates.Enabled = true;
btnCheckUpdates.Enabled = true;
lnkLblDirectDownload.Enabled = true;
lblANewVersionAva.Text = Localization.lblANewVersionAva + "\n" + Localization.lblANewVersionAva2;
lblNewVersion.Text = Localization.LastVersion + " " + newVersion;
}
else
{
btnCheckUpdates.Enabled = true;
btnInstallUpdates.Enabled = false;
btnDownloadUpdates.Enabled = false;
lblANewVersionAva.Text = Localization.NoNewVersion;
lnkLblDirectDownload.Enabled = true;
lblNewVersion.Text = Localization.LastVersion + " " + productVersion;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
lblNewVersion.Text = Localization.LastVersion + " " + Localization.ERROR_2 + " " + Localization.LatestVersionNotDetected;
lblANewVersionAva.Text = Localization.ERROR_2 + " " + Localization.LatestVersionNotDetected;
lnkLblDirectDownload.Enabled = true;
btnCheckUpdates.Enabled = true;
}
}
private void btnCheckUpdates_Click(object sender, EventArgs e)
{
CheckUpdate();
}
private void btnDownloadUpdates_Click(object sender, EventArgs e)
{
btnCheckUpdates.Enabled = false;
btnDownloadUpdates.Enabled = false;
Download.DownloadUpdates(urlDownload, path, WebDownloadFileCompleted, WebDownloadProgressChanged);
}
private void WebDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Minimum = 0;
double receive = double.Parse(e.BytesReceived.ToString());
FileSize = double.Parse(e.TotalBytesToReceive.ToString());
Percentage = receive / FileSize * 100;
lblStatus.Text = Localization.Downloading + $" { string.Format("{0:0.##}%", Percentage)}";
progressBar.Value = int.Parse(Math.Truncate(Percentage).ToString());
progressBar.Update();
}
private void WebDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
btnDownloadUpdates.Enabled = true;
}
else
{
lblStatus.Text = Localization.Downloaded + " " + "100%" + " " + Localization.TotalSize + $" {string.Format("{0:0.##} KB", FileSize / Percentage)}";
if (Hash.CheckHash(UrlSHA256, path, is64BitProcess))
{
lnkLblOpenFolder.Text = path;
lnkLblOpenFolder.Visible = true;
btnDownloadUpdates.Enabled = false;
btnInstallUpdates.Enabled = true;
}
else
{
MessageBox.Show(Localization.DownloadedFileDontTrue + "\n\n" + path, Localization.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
lnkLblOpenFolder.Text = path;
lnkLblOpenFolder.Visible = true;
btnDownloadUpdates.Enabled = false;
btnInstallUpdates.Enabled = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(Localization.VerificationFileNo + "\n\n" + ex.Message, Localization.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
lnkLblOpenFolder.Text = path;
lnkLblOpenFolder.Visible = true;
btnDownloadUpdates.Enabled = false;
btnInstallUpdates.Enabled = true;
}
}
private void btnInstallUpdates_Click(object sender, EventArgs e)
{
try
{
btnInstallUpdates.Enabled = false;
Install.InstallUpdates(path, productName);
Application.Restart();
}
catch (Exception ex)
{
btnInstallUpdates.Enabled = true;
MessageBox.Show(ex.Message);
}
}
private void lnkLblDirectDownload_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
Download.DirectDownload(UrlGitHubReleases + "latest", urlDownload);
lnkLblDirectDownload.LinkVisited = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateManagerForm_Load(object sender, EventArgs e)
{
CheckUpdate();
}
private void lnkLblOpenFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
lnkLblOpenFolder.LinkVisited = true;
string cmd = "explorer.exe";
string arg = "/select, " + path;
System.Diagnostics.Process.Start(cmd, arg);
}
}
}