-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDownload.cs
173 lines (148 loc) · 6.07 KB
/
Download.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
using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Reflection;
namespace SuperPad
{
public partial class Download : Form
{
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32")]
private static extern bool ShowWindowAsync(IntPtr hwnd, int a);
WebClient wc = new WebClient();
public Download()
{
InitializeComponent();
}
private void Download_Load(object sender, EventArgs e)
{
spsLbl.Visible = false;
md5Lbl.Visible = false;
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
lblStatus.Text = "Status: Downloading update from server";
using (System.Net.WebClient client = new System.Net.WebClient())
{
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(FileDownloadCompleted);
Uri url = new Uri("http://dl.supers0ft.us/superpad/superpadsetup.exe");
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(FileDownloadCompleted);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
wc.DownloadFileAsync(url, "superpadsetup.exe");
}
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private async void FileDownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
string md5checkhash = SuperPad.Properties.Settings.Default.md5;
md5Lbl.Text = SuperPad.Properties.Settings.Default.md5;
cancelBtn.Visible = false;
lblStatus.Text = "Status: Verifying package signature";
// Verify package integrity | check md5
string fileName = spsLbl.Text;
if (File.Exists("superpadsetup.exe"))
{
byte[] md5HashBytes = await Task.Run(() => ComputeMd5Hash(fileName));
md5Lbl.Text = ToHexadecimal(md5HashBytes);
if (!string.IsNullOrWhiteSpace(md5Lbl.Text))
{
if (md5Lbl.Text.ToUpper().Equals(md5checkhash))
{
lblStatus.Text = "Status: Checking for multiple instances";
await Task.Delay(1000);
lblStatus.Text = "Status: Launching update package";
int count = Process.GetProcessesByName("superpadsetup").Count();
if (count < 1)
Process.Start("superpadsetup.exe /SILENT");
else
{
var proc = Process.GetProcessesByName("superpadsetup").FirstOrDefault();
if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(proc.MainWindowHandle);
ShowWindowAsync(proc.MainWindowHandle, 3);
}
}
await Task.Delay(200);
Application.Exit();
}
else
{
lblStatus.Text = "Invalid MD5 Signature, Continue?";
cancelBtn.Text = "No";
cancelBtn.Visible = true;
yesBtn.Visible = true;
}
}
}
}
private void cancelBtn_Click(object sender, EventArgs e)
{
File.Delete("superpadsetup.exe");
this.Dispose();
}
private byte[] ComputeMd5Hash(string fileName)
{
byte[] result = null;
using (MD5 md5 = MD5.Create())
{
int bufferSize = 10 * 1024 * 1024; //10MB
using (var stream = new BufferedStream(File.OpenRead(fileName), bufferSize))
{
result = md5.ComputeHash(stream);
}
}
return result;
}
static string ToHexadecimal(byte[] source)
{
if (source == null) return string.Empty;
StringBuilder sb = new StringBuilder();
foreach (byte b in source)
{
sb.Append(b.ToString("X2")); // print byte as Hexadecimal string
}
return sb.ToString();
}
private async void yesBtn_Click(object sender, EventArgs e)
{
cancelBtn.Visible = false;
yesBtn.Visible = false;
lblStatus.Text = "Status: Checking for multiple instances";
await Task.Delay(1000);
lblStatus.Text = "Status: Launching update package";
int count = Process.GetProcessesByName("superpadsetup").Count();
string path11 = Application.StartupPath;
using (Process pProcess = new Process())
{
pProcess.StartInfo.FileName = $@"{path11}\\superpadsetup.exe";
pProcess.StartInfo.Arguments = "/SILENT /SP"; //argument
if (count < 1)
Process.Start("superpadsetup.exe");
else
{
var proc = Process.GetProcessesByName("superpadsetup").FirstOrDefault();
if (proc != null && proc.MainWindowHandle != IntPtr.Zero)
{
SetForegroundWindow(proc.MainWindowHandle);
ShowWindowAsync(proc.MainWindowHandle, 3);
}
}
await Task.Delay(200);
Application.Exit();
}
}
}
}