Repository navigation
Expand file tree
/
Copy pathOperatingSystem.cs
More file actions
507 lines (396 loc) · 21.2 KB
/
Copy pathOperatingSystem.cs
File metadata and controls
507 lines (396 loc) · 21.2 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Security;
namespace Alphaleonis.Win32
{
/// <summary>Static class providing access to information about the operating system under which the assembly is executing.</summary>
public static class OperatingSystem
{
#region OperatingSystem Name Enum
/// <summary>A set of flags that describe the named Windows versions.</summary>
/// <remarks>The values of the enumeration are ordered. A later released operating system version has a higher number, so comparisons between named versions are meaningful.</remarks>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Os")]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Os")]
public enum EnumOsName
{
/// <summary>A Windows version earlier than Windows 2000.</summary>
Earlier = -1,
/// <summary>Windows 2000 (Server or Professional).</summary>
Windows2000 = 0,
/// <summary>Windows XP.</summary>
WindowsXP = 1,
/// <summary>Windows Server 2003.</summary>
WindowsServer2003 = 2,
/// <summary>Windows Vista.</summary>
WindowsVista = 3,
/// <summary>Windows Server 2008.</summary>
WindowsServer2008 = 4,
/// <summary>Windows 7.</summary>
Windows7 = 5,
/// <summary>Windows Server 2008 R2.</summary>
WindowsServer2008R2 = 6,
/// <summary>Windows 8.</summary>
Windows8 = 7,
/// <summary>Windows Server 2012.</summary>
WindowsServer2012 = 8,
/// <summary>Windows 8.1.</summary>
Windows81 = 9,
/// <summary>Windows Server 2012 R2</summary>
WindowsServer2012R2 = 10,
/// <summary>Windows 10</summary>
Windows10 = 11,
/// <summary>Windows Server</summary>
WindowsServer = 12,
/// <summary>A later version of Windows than currently installed.</summary>
Later = 65535
}
#endregion // OperatingSystem Name Enum
#region ProcessorArchitecture Name enum
/// <summary>A set of flags to indicate the current processor architecture for which the operating system is targeted and running.</summary>
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Pa")]
[SuppressMessage("Microsoft.Design", "CA1028:EnumStorageShouldBeInt32")]
public enum EnumProcessorArchitecture
{
/// <summary>PROCESSOR_ARCHITECTURE_INTEL
/// <para>The system is running a 32-bit version of Windows.</para>
/// </summary>
X86 = 0,
/// <summary>PROCESSOR_ARCHITECTURE_IA64
/// <para>The system is running on a Itanium processor.</para>
/// </summary>
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Ia")]
[SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Ia")]
IA64 = 6,
/// <summary>PROCESSOR_ARCHITECTURE_AMD64
/// <para>The system is running a 64-bit version of Windows.</para>
/// </summary>
X64 = 9,
/// <summary>PROCESSOR_ARCHITECTURE_UNKNOWN
/// <para>Unknown architecture.</para>
/// </summary>
Unknown = 65535
}
#endregion // ProcessorArchitecture Name enum
#region Properties
#region IsServer
private static bool _isServer;
/// <summary>Gets a value indicating whether the operating system is a server operating system.</summary>
/// <value><see langword="true"/> if the current operating system is a server operating system; otherwise, <see langword="false"/>.</value>
public static bool IsServer
{
get
{
if (_servicePackVersion == null)
UpdateData();
return _isServer;
}
}
#endregion // IsServer
#region IsWow64Process
private static bool? _isWow64Process;
/// <summary>Gets a value indicating whether the current process is running under WOW64.</summary>
/// <value><see langword="true"/> if the current process is running under WOW64; otherwise, <see langword="false"/>.</value>
public static bool IsWow64Process
{
get
{
if (_isWow64Process == null)
{
bool value;
IntPtr processHandle = Process.GetCurrentProcess().Handle;
if (!NativeMethods.IsWow64Process(processHandle, out value))
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
// A pointer to a value that is set to TRUE if the process is running under WOW64.
// If the process is running under 32-bit Windows, the value is set to FALSE.
// If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE.
_isWow64Process = value;
}
return (bool) _isWow64Process;
}
}
#endregion // IsWow64Process
#region OSVersion
private static Version _osVersion;
/// <summary>Gets the numeric version of the operating system.</summary>
/// <value>The numeric version of the operating system.</value>
public static Version OSVersion
{
get
{
if (_osVersion == null)
UpdateData();
return _osVersion;
}
}
#endregion // OSVersion
#region VersionName
private static EnumOsName _enumOsName = EnumOsName.Later;
/// <summary>Gets the named version of the operating system.</summary>
/// <value>The named version of the operating system.</value>
public static EnumOsName VersionName
{
get
{
if (_servicePackVersion == null)
UpdateData();
return _enumOsName;
}
}
#endregion // VersionName
#region ProcessorArchitecture
private static EnumProcessorArchitecture _processorArchitecture;
/// <summary>Gets the processor architecture for which the operating system is targeted.</summary>
/// <value>The processor architecture for which the operating system is targeted.</value>
/// <remarks>If running under WOW64 this will return a 32-bit processor. Use <see cref="IsWow64Process"/> to determine if this is the case.</remarks>
public static EnumProcessorArchitecture ProcessorArchitecture
{
get
{
if (_servicePackVersion == null)
UpdateData();
return _processorArchitecture;
}
}
#endregion // ProcessorArchitecture
#region ServicePackVersion
private static Version _servicePackVersion;
/// <summary>Gets the version of the service pack currently installed on the operating system.</summary>
/// <value>The version of the service pack currently installed on the operating system.</value>
/// <remarks>Only the <see cref="System.Version.Major"/> and <see cref="System.Version.Minor"/> fields are used.</remarks>
public static Version ServicePackVersion
{
get
{
if (_servicePackVersion == null)
UpdateData();
return _servicePackVersion;
}
}
#endregion // ServicePackVersion
#endregion // Properties
#region Methods
#region IsAtLeast
/// <summary>Determines whether the operating system is of the specified version or later.</summary>
/// <returns><see langword="true"/> if the operating system is of the specified <paramref name="version"/> or later; otherwise, <see langword="false"/>.</returns>
/// <param name="version">The lowest version for which to return true.</param>
public static bool IsAtLeast(EnumOsName version)
{
return VersionName >= version;
}
/// <summary>Determines whether the operating system is of the specified version or later, allowing specification of a minimum service pack that must be installed on the lowest version.</summary>
/// <returns><see langword="true"/> if the operating system matches the specified <paramref name="version"/> with the specified service pack, or if the operating system is of a later version; otherwise, <see langword="false"/>.</returns>
/// <param name="version">The minimum required version.</param>
/// <param name="servicePackVersion">The major version of the service pack that must be installed on the minimum required version to return true. This can be 0 to indicate that no service pack is required.</param>
public static bool IsAtLeast(EnumOsName version, int servicePackVersion)
{
return IsAtLeast(version) && ServicePackVersion.Major >= servicePackVersion;
}
#endregion // IsAtLeast
#endregion // Methods
#region Private
[SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = "RtlGetVersion")]
private static void UpdateData()
{
var verInfo = new NativeMethods.RTL_OSVERSIONINFOEXW();
// Needed to prevent: System.Runtime.InteropServices.COMException:
// The data area passed to a system call is too small. (Exception from HRESULT: 0x8007007A)
verInfo.dwOSVersionInfoSize = Marshal.SizeOf(verInfo);
var sysInfo = new NativeMethods.SYSTEM_INFO();
NativeMethods.GetNativeSystemInfo(ref sysInfo);
// RtlGetVersion returns STATUS_SUCCESS (0).
if (NativeMethods.RtlGetVersion(ref verInfo))
throw new Win32Exception(Marshal.GetLastWin32Error(), "Function RtlGetVersion() failed to retrieve the operating system information.");
_osVersion = new Version(verInfo.dwMajorVersion, verInfo.dwMinorVersion, verInfo.dwBuildNumber);
_processorArchitecture = (EnumProcessorArchitecture) sysInfo.wProcessorArchitecture;
_servicePackVersion = new Version(verInfo.wServicePackMajor, verInfo.wServicePackMinor);
_isServer = verInfo.wProductType == NativeMethods.VER_NT_DOMAIN_CONTROLLER || verInfo.wProductType == NativeMethods.VER_NT_SERVER;
// RtlGetVersion: https://msdn.microsoft.com/en-us/library/windows/hardware/ff561910%28v=vs.85%29.aspx
// The following table summarizes the most recent operating system version numbers.
// Operating system Version number Other
// ================================================================================
// Windows 10 10.0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
// Windows Server 10.0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
// Windows 8.1 6.3 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
// Windows Server 2012 R2 6.3 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
// Windows 8 6.2 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
// Windows Server 2012 6.2 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
// Windows 7 6.1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
// Windows Server 2008 R2 6.1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
// Windows Server 2008 6.0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
// Windows Vista 6.0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
// Windows Server 2003 R2 5.2 GetSystemMetrics(SM_SERVERR2) != 0
// Windows Server 2003 5.2 GetSystemMetrics(SM_SERVERR2) == 0
// Windows XP 64-Bit Edition 5.2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (sysInfo.PaName == PaName.X64)
// Windows XP 5.1 Not applicable
// Windows 2000 5.0 Not applicable
// 10 == The lastest MajorVersion of Windows.
if (verInfo.dwMajorVersion > 10)
_enumOsName = EnumOsName.Later;
else
switch (verInfo.dwMajorVersion)
{
#region Version 10
case 10:
switch (verInfo.dwMinorVersion)
{
// Windows 10 or Windows Server
case 0:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION)
? EnumOsName.Windows10
: EnumOsName.WindowsServer;
break;
}
break;
#endregion // Version 10
#region Version 6
case 6:
switch (verInfo.dwMinorVersion)
{
// Windows Vista or Windows Server 2008
case 0:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION)
? EnumOsName.WindowsVista
: EnumOsName.WindowsServer2008;
break;
// Windows 7 or Windows Server 2008 R2
case 1:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION)
? EnumOsName.Windows7
: EnumOsName.WindowsServer2008R2;
break;
// Windows 8 or Windows Server 2012
case 2:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION)
? EnumOsName.Windows8
: EnumOsName.WindowsServer2012;
break;
// Windows 8.1 or Windows Server 2012 R2
case 3:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION)
? EnumOsName.Windows81
: EnumOsName.WindowsServer2012R2;
break;
default:
_enumOsName = EnumOsName.Later;
break;
}
break;
#endregion // Version 6
#region Version 5
case 5:
switch (verInfo.dwMinorVersion)
{
case 0:
_enumOsName = EnumOsName.Windows2000;
break;
case 1:
_enumOsName = EnumOsName.WindowsXP;
break;
case 2:
_enumOsName = (verInfo.wProductType == NativeMethods.VER_NT_WORKSTATION && _processorArchitecture == EnumProcessorArchitecture.X64)
? EnumOsName.WindowsXP
: (verInfo.wProductType != NativeMethods.VER_NT_WORKSTATION) ? EnumOsName.WindowsServer2003 : EnumOsName.Later;
break;
default:
_enumOsName = EnumOsName.Later;
break;
}
break;
#endregion // Version 5
default:
_enumOsName = EnumOsName.Earlier;
break;
}
}
#region P/Invoke members / NativeMethods
private static class NativeMethods
{
internal const short VER_NT_WORKSTATION = 1;
internal const short VER_NT_DOMAIN_CONTROLLER = 2;
internal const short VER_NT_SERVER = 3;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct RTL_OSVERSIONINFOEXW
{
public int dwOSVersionInfoSize;
public readonly int dwMajorVersion;
public readonly int dwMinorVersion;
public readonly int dwBuildNumber;
public readonly int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public readonly string szCSDVersion;
public readonly ushort wServicePackMajor;
public readonly ushort wServicePackMinor;
public readonly ushort wSuiteMask;
public readonly byte wProductType;
public readonly byte wReserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SYSTEM_INFO
{
public readonly ushort wProcessorArchitecture;
private readonly ushort wReserved;
public readonly uint dwPageSize;
public readonly IntPtr lpMinimumApplicationAddress;
public readonly IntPtr lpMaximumApplicationAddress;
public readonly IntPtr dwActiveProcessorMask;
public readonly uint dwNumberOfProcessors;
public readonly uint dwProcessorType;
public readonly uint dwAllocationGranularity;
public readonly ushort wProcessorLevel;
public readonly ushort wProcessorRevision;
}
/// <summary>The RtlGetVersion routine returns version information about the currently running operating system.</summary>
/// <returns>RtlGetVersion returns STATUS_SUCCESS.</returns>
/// <remarks>Available starting with Windows 2000.</remarks>
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule"), DllImport("ntdll.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool RtlGetVersion([MarshalAs(UnmanagedType.Struct)] ref RTL_OSVERSIONINFOEXW lpVersionInformation);
/// <summary>Retrieves information about the current system to an application running under WOW64.
/// If the function is called from a 64-bit application, it is equivalent to the GetSystemInfo function.
/// </summary>
/// <returns>This function does not return a value.</returns>
/// <remarks>To determine whether a Win32-based application is running under WOW64, call the <see cref="IsWow64Process"/> function.</remarks>
/// <remarks>Minimum supported client: Windows XP [desktop apps | Windows Store apps]</remarks>
/// <remarks>Minimum supported server: Windows Server 2003 [desktop apps | Windows Store apps]</remarks>
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
internal static extern void GetNativeSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);
/// <summary>Determines whether the specified process is running under WOW64.</summary>
/// <returns>
/// If the function succeeds, the return value is a nonzero value.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
/// </returns>
/// <remarks>Minimum supported client: Windows Vista, Windows XP with SP2 [desktop apps only]</remarks>
/// <remarks>Minimum supported server: Windows Server 2008, Windows Server 2003 with SP1 [desktop apps only]</remarks>
[SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
[DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWow64Process([In] IntPtr hProcess, [Out, MarshalAs(UnmanagedType.Bool)] out bool lpSystemInfo);
}
#endregion // P/Invoke members / NativeMethods
#endregion // Private
}
}