This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
OSVersionHelper.cs
178 lines (149 loc) · 6.67 KB
/
OSVersionHelper.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Security;
using Microsoft.Toolkit.Win32.UI.Controls.Interop.Win32;
using Windows.Foundation.Metadata;
using Windows.Security.EnterpriseData;
namespace Microsoft.Toolkit.Win32.UI.Controls.Interop
{
internal static class OSVersionHelper
{
private const string ContractName = "Windows.Foundation.UniversalApiContract";
[SecurityCritical]
static OSVersionHelper()
{
if (IsSince(WindowsVersions.Win10))
{
if (IsApiContractPresent(6))
{
Windows10Release = Windows10Release.April2018;
}
else if (IsApiContractPresent(5))
{
Windows10Release = Windows10Release.FallCreators;
}
else if (IsApiContractPresent(4))
{
Windows10Release = Windows10Release.Creators;
}
else if (IsApiContractPresent(3))
{
Windows10Release = Windows10Release.Anniversary;
}
else if (IsApiContractPresent(2))
{
Windows10Release = Windows10Release.Threshold2;
}
else if (IsApiContractPresent(1))
{
Windows10Release = Windows10Release.Threshold1;
}
else
{
Windows10Release = Windows10Release.Unknown;
}
}
}
internal static bool IsWindowsNt { get; } = Environment.OSVersion.Platform == PlatformID.Win32NT;
internal static bool EdgeExists { get; } = File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), ExternDll.EdgeHtml));
internal static bool IsWindows10 { get; } = IsWindowsNt && IsSince(WindowsVersions.Win10);
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 April 2018 Update (Redstone 4) or greater
/// </summary>
internal static bool IsWindows10April2018OrGreater => IsWindows10 && Windows10Release >= Windows10Release.April2018;
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 Fall Creators Update (Redstone 3) or greater
/// </summary>
internal static bool IsWindows10FallCreatorsOrGreater => IsWindows10 && Windows10Release >= Windows10Release.FallCreators;
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 Creators Update (Redstone 2) or greater
/// </summary>
internal static bool IsWindows10CreatorsOrGreater => IsWindows10 && Windows10Release >= Windows10Release.Creators;
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 Anniversary Update (Redstone 1) or greater
/// </summary>
internal static bool IsWindows10AnniversaryOrGreater => IsWindows10 && Windows10Release >= Windows10Release.Anniversary;
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 Threshold 2 or greater
/// </summary>
internal static bool IsWindows10Threshold2OrGreater => IsWindows10 && Windows10Release >= Windows10Release.Threshold2;
/// <summary>
/// Gets a value indicating whether the current OS is Windows 10 Threshold 1 or greater
/// </summary>
internal static bool IsWindows10Threshold1OrGreater => IsWindows10 && Windows10Release >= Windows10Release.Threshold1;
internal static bool IsWorkstation { get; } = !IsServer();
internal static bool UseWindowsInformationProtectionApi
{
[SecurityCritical]
get => Windows10Release >= Windows10Release.Anniversary && ProtectionPolicyManager.IsProtectionEnabled;
}
internal static Windows10Release Windows10Release { get; }
/// <summary>
/// Checks if OS is Windows 10 April 2018 or later, is a workstation, and Microsoft Edge exists.
/// </summary>
/// <exception cref="NotSupportedException">Not running correct OS or OS Version, or Microsoft Edge does not exist.</exception>
internal static void ThrowIfBeforeWindows10April2018()
{
if (IsWindows10April2018OrGreater && IsWorkstation && EdgeExists)
{
return;
}
throw new NotSupportedException(DesignerUI.E_NOTSUPPORTED_OS_RS4);
}
[SecurityCritical]
private static bool IsApiContractPresent(ushort majorVersion) => ApiInformation.IsApiContractPresent(ContractName, majorVersion);
[SecurityCritical]
private static bool IsServer()
{
var versionInfo = NativeMethods.RtlGetVersion();
return versionInfo.ProductType == ProductType.VER_NT_DOMAIN_CONTROLLER
|| versionInfo.ProductType == ProductType.VER_NT_SERVER;
}
[SecurityCritical]
internal static bool IsSince(WindowsVersions version)
{
int major;
int minor;
switch (version)
{
case WindowsVersions.Win7:
case WindowsVersions.Server2008R2:
major = 6;
minor = 1;
break;
case WindowsVersions.Win8:
case WindowsVersions.Server2012:
major = 6;
minor = 2;
break;
case WindowsVersions.Win81:
case WindowsVersions.Server2012R2:
major = 6;
minor = 3;
break;
case WindowsVersions.Win10:
case WindowsVersions.Server2016:
major = 10;
minor = 0;
break;
default:
throw new ArgumentException(DesignerUI.E_UNRECOGNIZED_OS, nameof(version));
}
// After 8.1 apps without manifest or are not manifested for 8.1/10 return 6.2 when using GetVersionEx.
// Need to use RtlGetVersion to get correct major/minor/build
var os = NativeMethods.RtlGetVersion();
if (os.MajorVersion > major)
{
return true;
}
if (os.MajorVersion == major)
{
return os.MinorVersion >= minor;
}
return false;
}
}
}