-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateCheck.cs
108 lines (97 loc) · 3.79 KB
/
UpdateCheck.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
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace RenamerNG
{
static class UpdateCheck
{
static long lastCheck;
static bool check;
static bool checkBeta;
static string version;
static string betaVersion;
public static long LastCheck
{
get
{
return lastCheck;
}
}
public static void Check(long lastCheckArg, bool checkArg, bool checkBetaArg, string versionArg, string betaVersionArg)
{
lastCheck = lastCheckArg;
check = checkArg;
checkBeta = checkBetaArg;
version = versionArg;
betaVersion = betaVersionArg;
if (check || checkBeta)
{
Thread t = new Thread(new ThreadStart(Checker));
t.Start();
}
}
private static int CompareVersion(string s1, string s2)
{
string[] parts1 = s1.Split('.');
string[] parts2 = s2.Split('.');
if (parts1.Length != 4 || parts2.Length != 4) throw new Exception("Invalid version strings.");
for (int i = 0; i < parts1.Length; i++)
{
if (int.Parse(parts1[i]) > int.Parse(parts2[i])) return 1;
if (int.Parse(parts1[i]) < int.Parse(parts2[i])) return -1;
}
return 0;
}
private static void Checker()
{
try
{
const string urlBase = @"http://www.albert.nu/programs/renamerng/";
const string urlVersion = @"version.txt";
const string urlProgram = "renamerng.zip";
const string urlBetaProgram = "renamerng-beta.zip";
HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)WebRequest.Create(urlBase + urlVersion)).GetResponse();
using (response)
{
StreamReader r = new StreamReader(response.GetResponseStream());
using (r)
{
string newVersion = r.ReadLine();
string newBetaVersion = r.ReadLine();
bool found = false;
if (check)
{
if (CompareVersion(newVersion, version) > 0)
{
found = true;
DialogResult res = MessageBox.Show("A new version is available. Download it now?", "Info", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
System.Diagnostics.Process.Start(urlBase + urlProgram);
}
}
}
if (!found && checkBeta)
{
if (CompareVersion(newBetaVersion, betaVersion) > 0)
{
found = true;
DialogResult res = MessageBox.Show("A new BETA version is available. Download it now?", "Info", MessageBoxButtons.YesNo);
if (res == DialogResult.Yes)
{
System.Diagnostics.Process.Start(urlBase + urlBetaProgram);
}
}
}
}
}
}
catch
{
//Do nothing, fail silently
}
}
}
}