-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateForm.cs
More file actions
224 lines (192 loc) · 9.29 KB
/
UpdateForm.cs
File metadata and controls
224 lines (192 loc) · 9.29 KB
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
using System.Diagnostics;
using System.IO.Compression;
using System.Net;
using System.Runtime.InteropServices;
using System.Xml;
namespace DownloadManagerInstaller
{
public partial class UpdateForm : Form
{
#region DLL Import
[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
protected override void OnHandleCreated(EventArgs e)
{
if (DwmSetWindowAttribute(Handle, 19, new[] { 1 }, 4) != 0)
DwmSetWindowAttribute(Handle, 20, new[] { 1 }, 4);
}
#endregion
public static readonly string installationPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\";
bool installing = false;
public UpdateForm()
{
InitializeComponent();
}
//Disable close button
private const int CP_DISABLE_CLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | CP_DISABLE_CLOSE_BUTTON;
return cp;
}
}
private void UpdateForm_Load(object sender, EventArgs e)
{
this.Show();
DownloadProgress xmlProgress = new DownloadProgress("https://raw.githubusercontent.com/Lilyy2565/app-update/main/DownloadManager.xml", System.IO.Path.GetTempPath(), "");
xmlProgress.ShowDialog();
string version = "";
string url = "";
string mandatory = "";
try
{
XmlDocument xml = new XmlDocument();
xml.Load(System.IO.Path.GetTempPath() + "DownloadManager.xml");
version = xml.DocumentElement.ParentNode.ChildNodes.Item(1).ChildNodes.Item(0).InnerText;
url = xml.DocumentElement.ParentNode.ChildNodes.Item(1).ChildNodes.Item(1).InnerText;
mandatory = xml.DocumentElement.ParentNode.ChildNodes.Item(1).ChildNodes.Item(2).InnerText;
}
catch
{
MessageBox.Show("An error occurred while reading the update XML file.", "Download Manager Setup - Malformed XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
progressBar1.Style = ProgressBarStyle.Blocks;
progressBar1.Value = 3;
ProgressBarColor.SetState(progressBar1, 2);
Environment.Exit(3);
}
string fileVersion = "";
try
{
fileVersion = FileVersionInfo.GetVersionInfo(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\DownloadManager.exe").FileVersion;
Debug.WriteLine(version + Environment.NewLine + url + Environment.NewLine + mandatory + Environment.NewLine + fileVersion);
}
catch
{
progressBar1.Style = ProgressBarStyle.Blocks;
progressBar1.Value = 3;
ProgressBarColor.SetState(progressBar1, 2);
MessageBox.Show("Setup could not retrieve the version of the existing Download Manager installation.\nEnsure setup is in the same directory as Download Manager and try again.", "Download Manager Setup", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(5);
}
if (fileVersion != version)
{
if (mandatory == "true")
{
DialogResult result = MessageBox.Show("A important update is available: " + version + ".\nThe current installed version is: " + fileVersion + ".\nPress OK to continue.", "Download Manager Setup - Mandatory Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
Update(url);
}
}
else
{
DialogResult result = MessageBox.Show("Version " + version + " is available.\nWould you like to install it?", "Download Manager Setup - Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Update(url);
}
else
{
Environment.Exit(2);
}
}
}
else
{
MessageBox.Show("You are on the latest version of Download Manager.", "Download Manager Setup", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.Exit(1);
}
}
void Update(string url)
{
installing = true;
label1.Text = "Updating...";
progressBar1.Style = ProgressBarStyle.Blocks;
if (url == "" || url.Length < 10)
{
MessageBox.Show("Setup could not retrieve the update URL.\nEnsure setup is in the same directory as Download Manager and try again.", "Download Manager Setup - Malformed XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(3);
}
else
{
try
{
// Download the update
DownloadProgress progress = new DownloadProgress(url, System.IO.Path.GetTempPath(), "");
progress.ShowDialog();
while (progress.downloading)
{
Application.DoEvents();
}
if (progress.error == true)
{
progressBar1.Value = 3;
throw new WebException("Failed to download: " + url);
}
progressBar1.Value = 1;
// Check if DownloadManager.exe is running
Process[] processes = Process.GetProcessesByName("DownloadManager");
if (processes.Length > 0)
{
foreach (Process process in processes)
{
Application.DoEvents();
process.Kill();
}
}
progressBar1.Value = 2;
// Install the update
ZipFile.ExtractToDirectory(System.IO.Path.GetTempPath() + "DownloadManager.zip", System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), true);
File.Delete(System.IO.Path.GetTempPath() + "DownloadManager.zip");
progressBar1.Value = 3;
// Finish install
label1.Text = "Update complete!";
DialogResult result = MessageBox.Show("Update complete.\nWould you like to open Download Manager?", "Download Manager Setup - Update Complete", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Process.Start(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\DownloadManager.exe");
}
Environment.Exit(0);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
ProgressBarColor.SetState(progressBar1, 2);
MessageBox.Show("A " + ex.GetType().ToString() + " occurred while installing.\nPlease create a bug report and reinstall Download Manager.", "Download Manager Setup - Install Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(4);
}
}
}
private void UpdateForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (installing == true)
{
DialogResult result = MessageBox.Show("Download Manager is currently installing.\nIf you cancel the installation now your Download Manager installation will be corrupted or incomplete.\nAre you sure you want to cancel the installation?", "Download Manager Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Environment.Exit(2);
}
else
{
e.Cancel = true;
}
}
else
{
Environment.Exit(2);
}
}
}
public static class ProgressBarColor
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
public static void SetState(this ProgressBar p, int state)
{
SendMessage(p.Handle, 1040, (IntPtr)state, IntPtr.Zero);
}
}
}