-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdater.cs
72 lines (71 loc) · 2.83 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
using DiskSpace.Forms;
using DiskSpace.Properties;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace DiskSpace
{
/// <summary>
/// Application update logic
/// </summary>
public static class Updater
{
/// <summary>
/// Check if application update is availble
/// </summary>
/// <returns>true|false</returns>
public static bool UpdateAvailable()
{
bool result = false;
try
{
using (WebClient webClient = new WebClient())
{
Uri versionFileUri = new Uri(Resources.CurrentVersionFileBaseUrl +
Resources.CurrentVersionFileName);
string localFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
Resources.CurrentVersionFileName);
webClient.DownloadFile(versionFileUri, localFile);
string infoDocument = File.ReadAllText(localFile);
string currentVer = Application.ProductVersion;
string version = infoDocument.Substring(
infoDocument.LastIndexOf(
Resources.AssemblyVersion, StringComparison.Ordinal)
).Substring(17, currentVer.Length);
result = currentVer != version;
if (!int.TryParse(version.Replace(".", ""),
NumberStyles.Integer, new NumberFormatInfo(), out int _))
{
Log.ErrorString = "Could not retrieve version online.";
MessageForm.DisplayMessage("Could not retrieve version online.");
}
else
{
MessageForm.LogAndDisplayMessage(result
? "New version available! Version " + version
: "Latest version already installed, version " + currentVer);
}
}
}
catch (WebException webException)
{
Log.Error = webException;
MessageForm.DisplayMessage("Could not check version online, see log for details.");
}
catch (IndexOutOfRangeException indexOutOfRangeException)
{
Log.Error = indexOutOfRangeException;
MessageForm.LogAndDisplayMessage("Could not check version online, invalid content.");
}
catch (Exception e)
{
Log.Error = e;
MessageForm.DisplayMessage("Could not check version online, fatal error. Restart application.");
throw;
}
return result;
}
}
}