-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.cs
101 lines (75 loc) · 3.51 KB
/
Updater.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Microsoft.Win32;
namespace ResourceCalculator
{
public partial class Updater : Form
{
public Updater()
{
InitializeComponent();
progressBar1.Visible = false;
var key = Registry.CurrentUser.CreateSubKey(@"Software\ResourceCalculator");
if (key?.GetValue("DarkMode")?.ToString() == "true")
{
BackColor = Color.FromArgb(255, 29, 29, 29);
label1.ForeColor = Color.FromArgb(255, 255, 255, 255);
ButtonUpdate.BackColor = Color.FromArgb(255, 29, 29, 29);
ButtonUpdate.ForeColor = Color.FromArgb(255, 255, 255, 255);
progressBar1.BackColor = Color.FromArgb(255, 29, 29, 29);
progressBar1.ForeColor = Color.FromArgb(255, 245, 245, 245);
}
else if (key?.GetValue("DarkMode")?.ToString() == "false")
{
BackColor = Color.FromArgb(255, 255, 255, 255);
label1.ForeColor = SystemColors.ControlText;
ButtonUpdate.BackColor = Color.FromArgb(255, 255, 255, 255);
ButtonUpdate.ForeColor = Color.FromArgb(255, 29, 29, 29);
}
}
public static string versionProg = Application.ProductVersion;
private void Button1_Click(object sender, EventArgs e)
{
File.Move("ResourceCalculator.exe", "ResourceCalculatorOldVersion.exe");
using (WebClient wc = new WebClient())
{
var Preurl = wc.DownloadString("https://raw.githubusercontent.com/BZHStudio/ResourceCalculator/master/url.txt");
string url = Preurl.ToString();
wc.OpenRead(url);
// string size = (Convert.ToDouble(wc.ResponseHeaders["Content-Length"]) / 1048576).ToString("#.# МБ");
string size = (Convert.ToDouble(wc.ResponseHeaders["Content-Length"]) / 1024).ToString("#.# КБ");
wc.DownloadProgressChanged += (s, a) =>
{
// label1.Text = $"Размер файла: {size}\nЗагружено: {a.ProgressPercentage}% ({((double)a.BytesReceived / 1048576).ToString("#.# МБ")})";
label1.Text = $"Размер файла: {size}\nЗагружено: {a.ProgressPercentage}% ({((double)a.BytesReceived / 1024).ToString("#.# КБ")})";
progressBar1.Value = a.ProgressPercentage;
};
string fileName = Path.Combine(Environment.CurrentDirectory, "ResourceCalculator.exe");
wc.DownloadFileAsync(new Uri(url), fileName);
}
ButtonUpdate.Enabled = false;
progressBar1.Visible = true;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == 100)
{
timer1.Enabled = false;
if (MessageBox.Show("Приложение будет автоматически обновлено и перезапущено.", "Обновление завершино!", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
{
Application.Restart();
}
}
}
}
}