-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsVersion.cs
258 lines (253 loc) · 9.73 KB
/
WindowsVersion.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using Windows.System.Profile;
namespace UniversalPlatformTools
{
/// <summary>
/// Represents a Windows build, it contains version and commercial information.
/// </summary>
public class WindowsVersion
{
private static readonly Version VERSION_RTM = new Version(10, 0, 10240, 0);
private static readonly Version VERSION_NOVEMBER = new Version(10, 0, 10586, 0);
private static readonly Version VERSION_ANNIVERSARY = new Version(10, 0, 14393, 0);
private static readonly Version VERSION_CREATORS = new Version(10, 0, 15063, 0);
private static readonly Version VERSION_FALLCREATORS = new Version(10, 0, 16299, 0);
private static readonly Version VERSION_APRIL2018 = new Version(10, 0, 17134, 0);
private static readonly Version VERSION_OCTOBER2018 = new Version(10, 0, 17763, 0);
private static readonly Version VERSION_MAY2019 = new Version(10, 0, 18362, 0);
//Add new versions here:
private static Version[] VERSIONS_SORTED = new[]
{
VERSION_RTM,
VERSION_NOVEMBER,
VERSION_ANNIVERSARY,
VERSION_CREATORS,
VERSION_FALLCREATORS,
VERSION_APRIL2018,
VERSION_OCTOBER2018,
VERSION_MAY2019
};
private static WindowsVersion current;
/// <summary>
/// Represents the current running version of Windows.
/// </summary>
public static WindowsVersion CurrentVersion
{
get
{
if (current == null)
{
InitializeCurrent();
}
return current;
}
}
/// <summary>
/// The Name of the Windows build.
/// </summary>
public string Name { get; set; }
/// <summary>
/// The actual version of the Windows build.
/// </summary>
public Version Build { get; set; }
/// <summary>
/// The commercial name given to the version of the Windows build.
/// </summary>
public string Version { get; set; }
/// <summary>
/// The version of the Universal API Contract.
/// This may be helpful to check if some APIs or Controls are present in the running Windows build.
/// </summary>
public int ApiContractLevel { get; set; }
/// <summary>
/// Returns wether this version is bigger or equal than the known Windows version.
/// </summary>
/// <param name="knownVersion">The known Windows version</param>
public bool IsCompatibleWith(KnownWindowsVersion knownVersion)
{
var compatibleVersion = GetKnownVersion(knownVersion);
return Build >= compatibleVersion.Build;
}
/// <summary>
/// Get version details based on a known Windows version.
/// </summary>
/// <param name="version">The known Windows version</param>
public static WindowsVersion GetKnownVersion(KnownWindowsVersion version)
{
switch (version)
{
default:
case KnownWindowsVersion.RTM:
{
return new WindowsVersion()
{
Name = "RTM",
Build = VERSION_RTM,
Version = "1506",
ApiContractLevel = 1,
};
}
case KnownWindowsVersion.NovemberUpdate:
{
return new WindowsVersion()
{
Name = "November Update",
Build = VERSION_NOVEMBER,
Version = "1511",
ApiContractLevel = 2,
};
}
case KnownWindowsVersion.AnniversaryUpdate:
{
return new WindowsVersion()
{
Name = "Anniversary Update",
Build = VERSION_ANNIVERSARY,
Version = "1607",
ApiContractLevel = 3,
};
}
case KnownWindowsVersion.CreatorsUpdate:
{
return new WindowsVersion()
{
Name = "Creators Update",
Build = VERSION_CREATORS,
Version = "1703",
ApiContractLevel = 4,
};
}
case KnownWindowsVersion.FallCreatosUpdate:
{
return new WindowsVersion()
{
Name = "Fall Creators Update",
Build = VERSION_CREATORS,
Version = "1709",
ApiContractLevel = 5,
};
}
case KnownWindowsVersion.April2018Update:
{
return new WindowsVersion()
{
Name = "April 2018 Update",
Build = VERSION_APRIL2018,
Version = "1803",
ApiContractLevel = 6,
};
}
case KnownWindowsVersion.October2018Update:
{
return new WindowsVersion()
{
Name = "OCTOBER 2018 Update",
Build = VERSION_OCTOBER2018,
Version = "1809",
ApiContractLevel = 7,
};
}
case KnownWindowsVersion.May2019Update:
{
return new WindowsVersion()
{
Name = "MAY 2019 Update",
Build = VERSION_MAY2019,
Version = "1903",
ApiContractLevel = 8,
};
}
}
}
private WindowsVersion()
{
}
private static void InitializeCurrent()
{
WindowsVersion baseVersion = GetBaseVersion();
current = new WindowsVersion
{
Name = baseVersion.Name,
ApiContractLevel = baseVersion.ApiContractLevel,
Build = GetOSVersion(),
Version = baseVersion.Version,
};
}
private static Version GetOSVersion()
{
string deviceFamilyVersion = AnalyticsInfo.VersionInfo.DeviceFamilyVersion;
ulong version = ulong.Parse(deviceFamilyVersion);
ulong major = (version & 0xFFFF000000000000L) >> 48;
ulong minor = (version & 0x0000FFFF00000000L) >> 32;
ulong build = (version & 0x00000000FFFF0000L) >> 16;
ulong revision = (version & 0x000000000000FFFFL);
return new Version((int)major, (int)minor, (int)build, (int)revision);
}
private static WindowsVersion GetBaseVersion()
{
Version actualBuild = GetOSVersion();
Version baseVersion = VERSION_RTM;
int i = 0;
while (i < VERSIONS_SORTED.Length - 1 && actualBuild >= VERSIONS_SORTED[i+1])
{
baseVersion = VERSIONS_SORTED[++i];
}
var knownVersion = GetKnownVersion(baseVersion);
return GetKnownVersion(knownVersion);
}
private static KnownWindowsVersion GetKnownVersion(Version version)
{
if (version == VERSION_RTM)
return KnownWindowsVersion.RTM;
if (version == VERSION_NOVEMBER)
return KnownWindowsVersion.NovemberUpdate;
if (version == VERSION_ANNIVERSARY)
return KnownWindowsVersion.AnniversaryUpdate;
if (version == VERSION_CREATORS)
return KnownWindowsVersion.CreatorsUpdate;
if (version == VERSION_FALLCREATORS)
return KnownWindowsVersion.FallCreatosUpdate;
if (version == VERSION_APRIL2018)
return KnownWindowsVersion.April2018Update;
return KnownWindowsVersion.October2018Update;
}
}
/// <summary>
/// Known Windows 10 versions
/// </summary>
public enum KnownWindowsVersion
{
/// <summary>
/// Represents the RTM. v10.0 Build 10.0.10240
/// </summary>
RTM,
/// <summary>
/// Represents the November Update. v10.0 Build 10.0.10586
/// </summary>
NovemberUpdate,
/// <summary>
/// Represents the Anniversary Update. v10.0 Build 10.0.14393
/// </summary>
AnniversaryUpdate,
/// <summary>
/// Represents the Creators Update. v10.0 Build 10.0.15063
/// </summary>
CreatorsUpdate,
/// <summary>
/// Represents the Fall Creators Update. v10.0 Build 16299
/// </summary>
FallCreatosUpdate,
/// <summary>
/// Represents the April (2018) Creators Update. v10.0 Build 17134
/// </summary>
April2018Update,
/// <summary>
/// Represents the October (2018) Creators Update. v10.0 Build 17134
/// </summary>
October2018Update,
/// <summary>
/// Represents the May 2019 Update. v10.0 Build 18362
/// </summary>
May2019Update,
}
}