-
Notifications
You must be signed in to change notification settings - Fork 0
/
WalkmanLibWinVersion.cs
184 lines (164 loc) · 7.03 KB
/
WalkmanLibWinVersion.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
using System;
using System.Management;
using System.Runtime.InteropServices;
// Credits:
// Main code: https://code.msdn.microsoft.com/windowsapps/Sample-to-demonstrate-how-495e69db
// Additional version tables:
// http://www.nirmaltv.com/2009/08/17/windows-os-version-numbers/
// which links to https://www.msigeek.com/442/windows-os-version-numbers
// https://stackoverflow.com/a/2819962/2999220
// https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_osversioninfoexa
// https://docs.microsoft.com/en-us/windows/desktop/SysInfo/operating-system-version
public enum WindowsVersion {
Windows1Point0,
Windows2Point0,
Windows3Point0,
WindowsNT3Point1,
WindowsNT3Point11,
WindowsNT3Point5,
WindowsNT3Point51,
Windows95,
WindowsNT4Point0,
Windows98,
Windows98SE,
WindowsME,
Windows2000,
WindowsXP,
WindowsXPProX64,
WindowsServer2003,
WindowsServer2003R2,
WindowsVista,
WindowsServer2008,
Windows7,
WindowsServer2008R2,
Windows8,
WindowsServer2012,
Windows8Point1,
WindowsServer2012R2,
Windows10,
WindowsServer2016
}
public partial class WalkmanLib {
/// <summary>Gets whether the current Operating System is a Windows Server version or not</summary>
/// <returns>True if current environment is a Server version, and False for a standard Workstation version</returns>
public static bool IsWindowsServer() {
// add a reference to System.Management.dll
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")) {
foreach (ManagementObject managementObject in searcher.Get()) {
// ProductType will be one of:
// 1: Workstation
// 2: Domain Controller
// 3: Server
uint productType = (uint)managementObject.GetPropertyValue("ProductType");
return productType != 1;
}
}
return false;
}
// this helps distinguish between Windows Server 2003 and Windows Server 2003 R2
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetSystemMetrics(int smIndex);
/// <summary>Gets the current Windows version. NOTE: To get an accurate version on Windows versions above 8,
/// you will need to embed a manifest as per https://msdn.microsoft.com/E7A1A16A-95B3-4B45-81AD-A19E33F15AE4
/// (https://docs.microsoft.com/en-us/windows/desktop/SysInfo/targeting-your-application-at-windows-8-1)</summary>
/// <returns>A Windows version of type <see cref="WindowsVersion"/></returns>
public static WindowsVersion GetWindowsVersion() {
Version currentVersion = Environment.OSVersion.Version;
switch (currentVersion.Major) {
case 1: {
return WindowsVersion.Windows1Point0;
}
case 2: {
return WindowsVersion.Windows2Point0;
}
case 3: {
if (currentVersion.Minor == 0) {
return WindowsVersion.Windows3Point0;
} else if (currentVersion.Minor == 10) {
return WindowsVersion.WindowsNT3Point1;
} else if (currentVersion.Minor == 11) {
return WindowsVersion.WindowsNT3Point11;
} else if (currentVersion.Minor == 5) {
return WindowsVersion.WindowsNT3Point5;
} else if (currentVersion.Minor == 51) {
return WindowsVersion.WindowsNT3Point51;
}
break;
}
case 4: {
if (currentVersion.Minor == 0) {
if (currentVersion.MinorRevision == 950) {
return WindowsVersion.Windows95;
} else if (currentVersion.MinorRevision == 1381) {
return WindowsVersion.WindowsNT4Point0;
}
} else if (currentVersion.Minor == 1 || currentVersion.Minor == 10) {
if (currentVersion.MinorRevision == 1998) {
return WindowsVersion.Windows98;
} else if (currentVersion.MinorRevision == 2222) {
return WindowsVersion.Windows98SE;
}
} else if (currentVersion.Minor == 90) {
return WindowsVersion.WindowsME;
}
break;
}
case 5: {
if (currentVersion.Minor == 0) {
return WindowsVersion.Windows2000;
} else if (currentVersion.Minor == 1) {
return WindowsVersion.WindowsXP;
} else if (currentVersion.Minor == 2) {
if (IsWindowsServer()) {
if (GetSystemMetrics(89) == 0) {
return WindowsVersion.WindowsServer2003;
} else {
return WindowsVersion.WindowsServer2003R2;
}
// Possibly also Windows Home Server - see https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_osversioninfoexa
} else {
return WindowsVersion.WindowsXPProX64;
}
}
break;
}
case 6: {
if (currentVersion.Minor == 0) {
if (IsWindowsServer()) {
return WindowsVersion.WindowsServer2008;
} else {
return WindowsVersion.WindowsVista;
}
} else if (currentVersion.Minor == 1) {
if (IsWindowsServer()) {
return WindowsVersion.WindowsServer2008R2;
} else {
return WindowsVersion.Windows7;
}
} else if (currentVersion.Minor == 2) {
if (IsWindowsServer()) {
return WindowsVersion.WindowsServer2012;
} else {
return WindowsVersion.Windows8;
}
} else if (currentVersion.Minor == 3) {
if (IsWindowsServer()) {
return WindowsVersion.WindowsServer2012R2;
} else {
return WindowsVersion.Windows8Point1;
}
}
break;
}
case 10: {
if (IsWindowsServer()) {
return WindowsVersion.WindowsServer2016;
} else {
return WindowsVersion.Windows10;
}
}
}
throw new InvalidOperationException(string.Format("Unrecognised Windows Version!{0}{0}VersionString: {1}{0}Version.ToString: {2}",
Environment.NewLine, Environment.OSVersion.VersionString, currentVersion.ToString()));
}
}