-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFormUpdater.cs
80 lines (68 loc) · 2.33 KB
/
FormUpdater.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace Updater
{
public partial class FormUpdater : Form
{
private readonly string _newPath;
private readonly string _exePath;
private readonly string _backupPath;
private WebClient _client;
private readonly string _downloadUrl;
public FormUpdater(string currentProgram, Version version, string downloadUrl)
{
InitializeComponent();
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
_newPath = currentProgram + ".new";
_exePath = currentProgram;
_backupPath = currentProgram + ".bak";
labelVersion.Text = version.ToString();
_downloadUrl = downloadUrl;
}
private void FormUpdater_Load(object sender, EventArgs e)
{
_client = new WebClient();
_client.DownloadFileCompleted += client_DownloadFileCompleted;
_client.DownloadProgressChanged += client_DownloadProgressChanged;
_client.DownloadFileAsync(new Uri(_downloadUrl), _newPath);
}
private void buttonCancel_Click(object sender, EventArgs e)
{
_client.CancelAsync();
}
private delegate void UpdateProgressValue(int value);
private void UpdateProgress(int value)
{
progressBarDownload.Value = value;
}
private void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Invoke(new UpdateProgressValue(UpdateProgress), e.ProgressPercentage);
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Cancelled)
{
if (File.Exists(_newPath))
{
File.Delete(_newPath);
}
Close();
}
else
{
if (File.Exists(_backupPath))
{
File.Delete(_backupPath);
}
File.Move(_exePath, _backupPath);
File.Move(_newPath, _exePath);
Application.Restart();
}
}
}
}