-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAutoUpdate.cs
More file actions
98 lines (90 loc) · 4.57 KB
/
AutoUpdate.cs
File metadata and controls
98 lines (90 loc) · 4.57 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
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
RMLib is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Globalization;
namespace RandM.RMLib
{
public static class AutoUpdate
{
public static string Comments { get; set; }
public static string Url { get; set; }
public static string Version { get; set; }
public static bool Available(Uri updateUrl)
{
// Don't auto-update in the debugger
if (Debugger.IsAttached)
{
// In debug mode we don't want to actually request the file
Version = ProcessUtils.ProductVersion;
Url = null;
Comments = "";
return false;
}
else
{
// TODO Display a form that shows we're doing something, otherwise it looks like the application hangs if it takes awhile to do the check
string AutoUpdateIniFile = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, "AutoUpdate.ini");
using (IniFile AutoUpdateIni = new IniFile(AutoUpdateIniFile))
{
// Check if there's an old installer to delete
string OldUrl = AutoUpdateIni.ReadString(ProcessUtils.ProductName, "URL", "");
if (OldUrl.Length > 0)
{
string OldInstaller = StringUtils.PathCombine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ProcessUtils.CompanyName, Path.GetFileName(OldUrl));
FileUtils.FileDelete(OldInstaller);
}
// Retrieve the new INI from the updater website
string NewIniFile = StringUtils.LFtoCRLF(WebUtils.HttpGet(AutoUpdateIni.ReadString("config", "URL", updateUrl.ToString()) + "?Name=" + WebUtils.UrlEncode(ProcessUtils.ProductName) + "&Version=" + WebUtils.UrlEncode(ProcessUtils.ProductVersion)));
if (!string.IsNullOrEmpty(NewIniFile))
{
// Save and re-open the new INI
Directory.CreateDirectory(Path.GetDirectoryName(AutoUpdateIniFile));
FileUtils.FileWriteAllText(AutoUpdateIniFile, NewIniFile);
using (IniFile NewIni = new IniFile(AutoUpdateIniFile))
{
// Read the version, url, and comments from the latest ini
Version = NewIni.ReadString(ProcessUtils.ProductName, "version", ProcessUtils.ProductVersion);
Url = NewIni.ReadString(ProcessUtils.ProductName, "URL", "");
int CommentCount = NewIni.ReadInt32(ProcessUtils.ProductName, "comments", 0);
if (CommentCount > 0)
{
Comments = NewIni.ReadString(ProcessUtils.ProductName, "comment1", "");
for (int i = 2; i <= CommentCount; i++)
{
Comments += Environment.NewLine + NewIni.ReadString(ProcessUtils.ProductName, "comment" + i.ToString(), "");
}
}
else
{
Comments = "";
}
}
}
else
{
// The new INI was not found
Version = ProcessUtils.ProductVersion;
Url = null;
Comments = "";
}
}
return (ProcessUtils.ProductVersion != Version);
}
}
}
}