-
Notifications
You must be signed in to change notification settings - Fork 0
/
VCInstaller.cs
75 lines (70 loc) · 2.54 KB
/
VCInstaller.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
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace MowerUpdater;
internal class VCInstaller : IDepInstaller
{
static Regex VersionRegex = new(@"(\d{4})(-(\d{4}))?", RegexOptions.Compiled);
static Regex VCRegistryItemRegex = new(@"VC,redist\.(.*),(.*),(\d+.\d+),bundle", RegexOptions.Compiled);
public string Name => "VC++ 2019";
public string Version => throw new NotImplementedException();
public bool CheckIfInstalled()
{
if (MsiHelper.CheckIfInstalled("Microsoft Visual C++ 2015-")) return true;
try
{
var parentKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies");
var keyname = parentKey.GetSubKeyNames()
.Where(name => VCRegistryItemRegex.IsMatch(name))
.FirstOrDefault();
var key = parentKey.OpenSubKey(keyname);
var display_name = key.GetValue("DisplayName") as string ?? throw new InvalidOperationException("未获取到已安装的VC版本");
var match = VersionRegex.Match(display_name);
if (match.Groups[3].Success)
{
var l = int.Parse(match.Groups[1].Value);
var r = int.Parse(match.Groups[3].Value);
return l <= 2019 && 2019 <= r;
}
else
{
return match.Groups[1].Value.Contains("2019");
}
}
catch
{
return false;
}
}
public async Task Install(HttpClient client, CancellationToken token = default)
{
string path, url;
if (Environment.Is64BitOperatingSystem)
{
path = Path.Combine(Path.GetTempPath(), "MowerUpdater", "vc_redist.x64.exe");
url = "https://aka.ms/vs/17/release/vc_redist.x64.exe";
}
else
{
path = Path.Combine(Path.GetTempPath(), "MowerUpdater", "vc_redist.x86.exe");
url = "https://aka.ms/vs/17/release/vc_redist.x86.exe";
}
await FileDownloader.EnsureDownloaded(client, url, path, token: token);
using var proc = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = path,
Arguments = "/install /quiet /norestart",
},
};
proc.Start();
proc.WaitForExit();
}
}