/
OSInfo.cs
864 lines (800 loc) · 31.8 KB
/
OSInfo.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Runtime.InteropServices;
namespace OSInfo
{
/// <summary>
/// OSInfo Class
/// </summary>
public class OSInfo
{
#region win32 API definitions
[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szCSDVersion;
public short wServicePackMajor;
public short wServicePackMinor;
public short wSuiteMask;
public byte wProductType;
public byte wReserved;
}
[DllImport("kernel32.dll")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
[DllImport("kernel32.dll")]
private static extern bool GetProductInfo(
[In] int dwOSMajorVersion,
[In] int dwOSMinorVersion,
[In] int dwSpMajorVersion,
[In] int dwSpMinorVersion,
[Out] out int pdwReturnedProductType);
[DllImport("user32.dll")]
private static extern bool GetSystemMetrics([In] int nIndex);
[DllImport("kernel32.dll")]
private static extern bool VerifyVersionInfo(ref OSVERSIONINFOEX osVersionInfo, [In] uint dwTypeMask, [In] UInt64 dwlConditionMask);
[DllImport("kernel32.dll")]
private static extern ulong VerSetConditionMask([In] ulong dwlConditionMask, [In] uint dwTypeBitMask,
[In] byte dwConditionMask);
#endregion
#region Private Constants
//wProductType ( http://msdn.microsoft.com/en-us/library/ms724833(VS.85).aspx )
private const int NT_WORKSTATION = 1;
private const int NT_DOMAIN_CONTROLLER = 2;
private const int NT_SERVER = 3;
//SuiteMask ( http://msdn.microsoft.com/en-us/library/ms724833(VS.85).aspx )
private const int VER_SUITE_SMALLBUSINESS = 0x00000001;
private const int VER_SUITE_ENTERPRISE = 0x00000002;
private const int VER_SUITE_BACKOFFICE = 0x00000004;
private const int VER_SUITE_TERMINAL = 0x00000010;
private const int VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x00000020;
private const int VER_SUITE_EMBEDDEDNT = 0x00000040;
private const int VER_SUITE_DATACENTER = 0x00000080;
private const int VER_SUITE_SINGLEUSERTS = 0x00000100;
private const int VER_SUITE_PERSONAL = 0x00000200;
private const int VER_SUITE_BLADE = 0x00000400;
private const int VER_SUITE_STORAGE_SERVER = 0x00002000;
private const int VER_SUITE_COMPUTE_SERV = 0x00004000;
private const int VER_SUITE_WH_SERVER = 0x00008000;
//ProductType ( http://msdn.microsoft.com/en-us/library/ms724358(VS.85).aspx )
private const int PRODUCT_BUSINESS = 0x00000006;
private const int PRODUCT_BUSINESS_N = 0x00000010;
private const int PRODUCT_CLUSTER_SERVER = 0x00000012;
private const int PRODUCT_DATACENTER_SERVER = 0x00000008;
private const int PRODUCT_DATACENTER_SERVER_CORE = 0x0000000C;
private const int PRODUCT_DATACENTER_SERVER_CORE_V = 0x00000027;
private const int PRODUCT_DATACENTER_SERVER_V = 0x00000025;
private const int PRODUCT_ENTERPRISE = 0x00000004;
private const int PRODUCT_ENTERPRISE_E = 0x00000046;
private const int PRODUCT_ENTERPRISE_N = 0x0000001B;
private const int PRODUCT_ENTERPRISE_SERVER = 0x0000000A;
private const int PRODUCT_ENTERPRISE_SERVER_CORE = 0x0000000E;
private const int PRODUCT_ENTERPRISE_SERVER_CORE_V = 0x00000029;
private const int PRODUCT_ENTERPRISE_SERVER_IA64 = 0x0000000F;
private const int PRODUCT_ENTERPRISE_SERVER_V = 0x00000026;
private const int PRODUCT_HOME_BASIC = 0x00000002;
private const int PRODUCT_HOME_BASIC_E = 0x00000043;
private const int PRODUCT_HOME_BASIC_N = 0x00000005;
private const int PRODUCT_HOME_PREMIUM = 0x00000003;
private const int PRODUCT_HOME_PREMIUM_E = 0x00000044;
private const int PRODUCT_HOME_PREMIUM_N = 0x0000001A;
private const int PRODUCT_HYPERV = 0x0000002A;
private const int PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT = 0x0000001E;
private const int PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING = 0x00000020;
private const int PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY = 0x0000001F;
private const int PRODUCT_PROFESSIONAL = 0x00000030;
private const int PRODUCT_PROFESSIONAL_E = 0x00000045;
private const int PRODUCT_PROFESSIONAL_N = 0x00000031;
private const int PRODUCT_SERVER_FOR_SMALLBUSINESS = 0x00000018;
private const int PRODUCT_SERVER_FOR_SMALLBUSINESS_V = 0x00000023;
private const int PRODUCT_SERVER_FOUNDATION = 0x00000021;
private const int PRODUCT_SMALLBUSINESS_SERVER = 0x00000009;
private const int PRODUCT_STANDARD_SERVER = 0x00000007;
private const int PRODUCT_STANDARD_SERVER_CORE = 0x0000000D;
private const int PRODUCT_STANDARD_SERVER_CORE_V = 0x00000028;
private const int PRODUCT_STANDARD_SERVER_V = 0x00000024;
private const int PRODUCT_STARTER = 0x0000000B;
private const int PRODUCT_STARTER_E = 0x00000042;
private const int PRODUCT_STARTER_N = 0x0000002F;
private const int PRODUCT_STORAGE_ENTERPRISE_SERVER = 0x00000017;
private const int PRODUCT_STORAGE_EXPRESS_SERVER = 0x00000014;
private const int PRODUCT_STORAGE_STANDARD_SERVER = 0x00000015;
private const int PRODUCT_STORAGE_WORKGROUP_SERVER = 0x00000016;
private const int PRODUCT_UNDEFINED = 0x00000000;
private const int PRODUCT_ULTIMATE = 0x00000001;
private const int PRODUCT_ULTIMATE_E = 0x00000047;
private const int PRODUCT_ULTIMATE_N = 0x0000001C;
private const int PRODUCT_WEB_SERVER = 0x00000011;
private const int PRODUCT_WEB_SERVER_CORE = 0x0000001D;
//Type bitmask ( http://msdn.microsoft.com/en-gb/library/ms725494(vs.85).aspx )
private const int VER_MINORVERSION = 0x0000001;
private const int VER_MAJORVERSION = 0x0000002;
private const int VER_BUILDVERSION = 0x0000004;
private const int VER_PLATFORMID = 0x0000008;
private const int VER_SERVICEPACKMINOR = 0x0000010;
private const int VER_SERVICEPACKMAJOR = 0x0000020;
private const int VER_SUITENAME = 0x0000040;
private const int VER_PRODUCT_TYPE = 0x0000080;
//Condition bitmask ( http://msdn.microsoft.com/en-gb/library/ms725494(vs.85).aspx )
private const int VER_EQUAL = 1;
private const int VER_GREATER = 2;
private const int VER_GREATER_EQUAL = 3;
private const int VER_LESS = 4;
private const int VER_LESS_EQUAL = 5;
private const int VER_AND = 6; // only for wSuiteMask
private const int VER_OR = 7; // only for wSuiteMask
//sysMetrics ( http://msdn.microsoft.com/en-us/library/ms724385(VS.85).aspx )
private const int SM_TABLETPC = 86;
private const int SM_MEDIACENTER = 87;
private const int SM_STARTER = 88;
private const int SM_SERVERR2 = 89;
#endregion
#region enums
/// <summary>
/// List of all operating systems
/// </summary>
public enum OSList
{
///<summary>
/// Windows 95/98, NT4.0, 2000
///</summary>
Windows2000andPrevious,
///<summary>
/// Windows XP x86
///</summary>
WindowsXp,
///<summary>
/// Windows XP x64
///</summary>
WindowsXp64,
///<summary>
/// Windows Vista
///</summary>
WindowsVista,
///<summary>
/// Windows 7
///</summary>
Windows7,
///<summary>
/// Windows 8
///</summary>
Windows8,
///<summary>
/// Windows 2003 Server
///</summary>
Windows2003,
///<summary>
/// Windows 2003 R2 Server
///</summary>
Windows2003R2,
///<summary>
/// Windows 2008 Server
///</summary>
Windows2008,
///<summary>
/// Windows 2008 R2 Server
///</summary>
Windows2008R2,
///<summary>
/// Windows 2012 Server
///</summary>
Windows2012
}
/// <summary>
/// List of available status of current OS
/// </summary>
public enum OsSupport
{
/// <summary>
/// Blocked: will cause an immediate exit of the program
/// </summary>
Blocked = 0,
/// <summary>
/// FullySupported: self explanatory
/// </summary>
FullySupported = 1,
/// <summary>
/// NotSupported: officially not supported, will log/display a warning
/// </summary>
NotSupported = 2
}
#endregion
#region Public Methods
/// <summary>
/// Returns the product type of the operating system running on this computer.
/// </summary>
/// <returns>A string containing the the operating system product type.</returns>
public static string GetOSProductType()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
if (!GetVersionEx(ref osVersionInfo)) return string.Empty;
switch (OSMajorVersion)
{
case 4:
if (OSProductType == NT_WORKSTATION)
{
// Windows NT 4.0 Workstation
return " Workstation";
}
if (OSProductType == NT_SERVER)
{
// Windows NT 4.0 Server
return " Server";
}
return string.Empty;
case 5:
if (GetSystemMetrics(SM_MEDIACENTER))
{
return " Media Center";
}
if (GetSystemMetrics(SM_TABLETPC))
{
return " Tablet PC";
}
if (OSProductType == NT_WORKSTATION)
{
if ((osVersionInfo.wSuiteMask & VER_SUITE_EMBEDDEDNT) == VER_SUITE_EMBEDDEDNT)
{
//Windows XP Embedded
return " Embedded";
}
return (osVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL ? " Home" : " Professional";
// Windows XP / Windows 2000 Professional
}
if (OSProductType == NT_SERVER || OSProductType == NT_DOMAIN_CONTROLLER)
{
if (OSMinorVersion == 0)
{
if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
{
// Windows 2000 Datacenter Server
return " Datacenter Server";
}
if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
{
// Windows 2000 Advanced Server
return " Advanced Server";
}
// Windows 2000 Server
return " Server";
}
if (OSMinorVersion == 2)
{
if ((osVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
{
// Windows Server 2003 Datacenter Edition
return " Datacenter Edition";
}
if ((osVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
{
// Windows Server 2003 Enterprise Edition
return " Enterprise Edition";
}
if ((osVersionInfo.wSuiteMask & VER_SUITE_STORAGE_SERVER) == VER_SUITE_STORAGE_SERVER)
{
// Windows Server 2003 Storage Edition
return " Storage Edition";
}
if ((osVersionInfo.wSuiteMask & VER_SUITE_COMPUTE_SERV) == VER_SUITE_COMPUTE_SERV)
{
// Windows Server 2003 Compute Cluster Edition
return " Compute Cluster Edition";
}
if ((osVersionInfo.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
{
// Windows Server 2003 Web Edition
return " Web Edition";
}
// Windows Server 2003 Standard Edition
return " Standard Edition";
}
}
break;
case 6:
int strProductType;
GetProductInfo(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, 0, 0, out strProductType);
switch (strProductType)
{
case PRODUCT_ULTIMATE:
case PRODUCT_ULTIMATE_E:
case PRODUCT_ULTIMATE_N:
return "Ultimate Edition";
case PRODUCT_PROFESSIONAL:
case PRODUCT_PROFESSIONAL_E:
case PRODUCT_PROFESSIONAL_N:
return "Professional";
case PRODUCT_HOME_PREMIUM:
case PRODUCT_HOME_PREMIUM_E:
case PRODUCT_HOME_PREMIUM_N:
return "Home Premium Edition";
case PRODUCT_HOME_BASIC:
case PRODUCT_HOME_BASIC_E:
case PRODUCT_HOME_BASIC_N:
return "Home Basic Edition";
case PRODUCT_ENTERPRISE:
case PRODUCT_ENTERPRISE_E:
case PRODUCT_ENTERPRISE_N:
case PRODUCT_ENTERPRISE_SERVER_V:
return "Enterprise Edition";
case PRODUCT_BUSINESS:
case PRODUCT_BUSINESS_N:
return "Business Edition";
case PRODUCT_STARTER:
case PRODUCT_STARTER_E:
case PRODUCT_STARTER_N:
return "Starter Edition";
case PRODUCT_CLUSTER_SERVER:
return "Cluster Server Edition";
case PRODUCT_DATACENTER_SERVER:
case PRODUCT_DATACENTER_SERVER_V:
return "Datacenter Edition";
case PRODUCT_DATACENTER_SERVER_CORE:
case PRODUCT_DATACENTER_SERVER_CORE_V:
return "Datacenter Edition (core installation)";
case PRODUCT_ENTERPRISE_SERVER:
return "Enterprise Edition";
case PRODUCT_ENTERPRISE_SERVER_CORE:
case PRODUCT_ENTERPRISE_SERVER_CORE_V:
return "Enterprise Edition (core installation)";
case PRODUCT_ENTERPRISE_SERVER_IA64:
return "Enterprise Edition for Itanium-based Systems";
case PRODUCT_SMALLBUSINESS_SERVER:
return "Small Business Server";
//case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
// return "Small Business Server Premium Edition";
case PRODUCT_SERVER_FOR_SMALLBUSINESS:
case PRODUCT_SERVER_FOR_SMALLBUSINESS_V:
return "Windows Essential Server Solutions";
case PRODUCT_STANDARD_SERVER:
case PRODUCT_STANDARD_SERVER_V:
return "Standard Edition";
case PRODUCT_STANDARD_SERVER_CORE:
case PRODUCT_STANDARD_SERVER_CORE_V:
return "Standard Edition (core installation)";
case PRODUCT_WEB_SERVER:
case PRODUCT_WEB_SERVER_CORE:
return "Web Server Edition";
case PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT:
case PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING:
case PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY:
return "Windows Essential Business Server ";
case PRODUCT_STORAGE_ENTERPRISE_SERVER:
case PRODUCT_STORAGE_EXPRESS_SERVER:
case PRODUCT_STORAGE_STANDARD_SERVER:
case PRODUCT_STORAGE_WORKGROUP_SERVER:
return "Storage Server";
}
break;
}
return string.Empty;
}
/// <summary>
/// Returns the service pack information of the operating system running on this computer.
/// </summary>
/// <returns>A string containing the the operating system service pack information.</returns>
public static string GetOSServicePack()
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
return !GetVersionEx(ref osVersionInfo) ? string.Empty : osVersionInfo.szCSDVersion;
}
/// <summary>
/// Returns the name of the operating system running on this computer.
/// </summary>
/// <returns>A string containing the the operating system name.</returns>
public static string GetOSNameString()
{
OperatingSystem osInfo = Environment.OSVersion;
string osName = "UNKNOWN";
switch (osInfo.Platform)
{
case PlatformID.Win32Windows:
{
switch (OSMinorVersion)
{
case 0:
{
osName = "Windows 95";
break;
}
case 10:
{
osName = osInfo.Version.Revision.ToString() == "2222A" ? "Windows 98 Second Edition" : "Windows 98";
break;
}
case 90:
{
osName = "Windows Me";
break;
}
}
break;
}
case PlatformID.Win32NT:
{
switch (OSMajorVersion)
{
case 3:
{
osName = "Windows NT 3.51";
break;
}
case 4:
{
osName = "Windows NT 4.0";
break;
}
case 5:
{
switch (OSMinorVersion)
{
case 0:
osName = "Windows 2000";
break;
case 1:
osName = "Windows XP";
break;
case 2:
if (OSProductType == NT_WORKSTATION)
{
osName = "WindowsXP x64";
}
else
{
osName = GetSystemMetrics(SM_SERVERR2) ? "Windows Server 2003 R2" : "Windows Server 2003";
}
break;
}
break;
}
case 6:
{
switch (OSMinorVersion)
{
case 0:
osName = OSProductType == NT_WORKSTATION ? "Windows Vista" : "Windows 2008";
break;
case 1:
osName = OSProductType == NT_WORKSTATION ? "Windows 7" : "Windows 2008 R2";
break;
case 2:
osName = OSProductType == NT_WORKSTATION ? "Windows 8" : "Windows 2012";
break;
}
break;
}
}
break;
}
}
if (!IsOSAsReported(OSMajorVersion, OSMinorVersion, OSBuildVersion, OSProductType, (short)OSServicePackMajor))
{
osName = "Compatibilty Mode: " + osName;
}
return osName;
}
/// <summary>
/// Returns the name of the operating system running on this computer.
/// </summary>
/// <returns>A string containing the the operating system name.</returns>
public static OSList GetOSName()
{
switch (OsVersionInt())
{
case 51:
return OSList.WindowsXp;
case 52:
if (OSProductType == NT_WORKSTATION)
{
return OSList.WindowsXp64;
}
return GetSystemMetrics(SM_SERVERR2) ? OSList.Windows2003R2 : OSList.Windows2003;
case 60:
return OSProductType == NT_WORKSTATION ? OSList.WindowsVista : OSList.Windows2008;
case 61:
return OSProductType == NT_WORKSTATION ? OSList.Windows7 : OSList.Windows2008R2;
case 62:
return OSProductType == NT_WORKSTATION ? OSList.Windows8 : OSList.Windows2012;
}
return OSList.Windows2000andPrevious;
}
/// <summary>
/// Return a full version string, es.: "Windows XP ( Servicepack 2 ) [5.1.0000]"
/// </summary>
/// <returns>A string rappresenting a fully displayable version</returns>
public static string GetOSDisplayVersion()
{
string servicePack = GetOSServicePack();
if (!string.IsNullOrEmpty(servicePack))
{
servicePack = " ( " + servicePack + " )";
}
return GetOSNameString() + servicePack + " [" + OSVersion + "]";
}
/// <summary>
/// Return a value that indicate if the OS is blocked, supported, or officially unsupported
/// </summary>
public static OsSupport GetOSSupported()
{
if (VerifyDesktopOSMinRequirement(5, 1, 2600, NT_WORKSTATION, 3))
{ // XP SP3
return OsSupport.FullySupported;
}
if (VerifyDesktopOSMinRequirement(6, 0, 6000, NT_WORKSTATION, 2))
{ // Vista SP2
return OsSupport.FullySupported;
}
if (VerifyDesktopOSMinRequirement(6, 1, 7600, NT_WORKSTATION, 0))
{ // Win7 RTM
return OsSupport.FullySupported;
}
if (VerifyDesktopOSMinRequirement(6, 2, 9200, NT_WORKSTATION, 0))
{ // Windows 8 RTM
return OsSupport.FullySupported;
}
if (IsServer())
{ // any server OS
return OsSupport.NotSupported;
}
return OsSupport.Blocked;
}
/// <summary>
/// Return if running on XP or later
/// </summary>
/// <returns>true means XP or later</returns>
/// <returns>false means 2000 or previous</returns>
public static bool XpOrLater()
{
return VerifyVersionGreaterEqual(5, 1);
}
/// <summary>
/// Return if running on XP 64 or later
/// </summary>
/// <returns>true means XP 64 or later</returns>
/// <returns>false means XP or previous</returns>
public static bool Xp64OrLater()
{
return VerifyVersionGreaterEqual(5, 2);
}
/// <summary>
/// Return if running on Vista or later
/// </summary>
/// <returns>true means Vista or later</returns>
/// <returns>false means Xp or previous</returns>
public static bool VistaOrLater()
{
return VerifyVersionGreaterEqual(6, 0);
}
/// <summary>
/// Return if running on Windows7 or later
/// </summary>
/// <returns>true means Windows7 or later</returns>
/// <returns>false means Vista or previous</returns>
public static bool Win7OrLater()
{
return VerifyVersionGreaterEqual(6, 1);
}
/// <summary>
/// Return if running on Windows8 or later
/// </summary>
/// <returns>true means Windows8 or later</returns>
/// <returns>false means Win7 or previous</returns>
public static bool Win8OrLater()
{
return VerifyVersionGreaterEqual(6, 2);
}
/// <summary>
/// Return a numeric value rappresenting OS version
/// </summary>
/// <returns>(OSMajorVersion * 10 + OSMinorVersion)</returns>
public static int OsVersionInt()
{
return (OSMajorVersion * 10 + OSMinorVersion);
}
#endregion
#region Public Properties
/// <summary>
/// Gets the full version of the operating system running on this computer.
/// </summary>
public static string OSVersion
{
get { return Environment.OSVersion.Version.ToString(); }
}
/// <summary>
/// Gets the major version of the operating system running on this computer.
/// </summary>
public static int OSMajorVersion
{
get { return Environment.OSVersion.Version.Major; }
}
/// <summary>
/// Gets the minor version of the operating system running on this computer.
/// </summary>
public static int OSMinorVersion
{
get { return Environment.OSVersion.Version.Minor; }
}
/// <summary>
/// Gets the build version of the operating system running on this computer.
/// </summary>
public static int OSBuildVersion
{
get { return Environment.OSVersion.Version.Build; }
}
/// <summary>
/// Gets the revision version of the operating system running on this computer.
/// </summary>
public static int OSRevisionVersion
{
get { return Environment.OSVersion.Version.Revision; }
}
/// <summary>
/// Gets the main version of the service pack running on this computer.
/// </summary>
public static int OSServicePackMajor
{
get
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
if (!GetVersionEx(ref osVersionInfo)) return -1;
return osVersionInfo.wServicePackMajor;
}
}
/// <summary>
/// Gets the main version of the service pack running on this computer.
/// </summary>
public static int OSServicePackMinor
{
get
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
return !GetVersionEx(ref osVersionInfo) ? -1 : osVersionInfo.wServicePackMinor;
}
}
/// <summary>
/// Gets the string description of the service pack running on this computer.
/// </summary>
public static string OSServicePackDesc
{
get
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
return !GetVersionEx(ref osVersionInfo) ? String.Empty : osVersionInfo.szCSDVersion;
}
}
/// <summary>
/// Gets the product type of the operating system running on this computer.
/// </summary>
public static byte OSProductType
{
get
{
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof (OSVERSIONINFOEX));
if (!GetVersionEx(ref osVersionInfo)) return 0x0;
return osVersionInfo.wProductType;
}
}
#endregion
#region private methods
/// <summary>
/// Checks if OS is later then major / minor version
/// </summary>
/// <param name="majorVersion">Major OS version</param>
/// <param name="minorVersion">Minor OS version</param>
/// <returns>True if OS is later than version supplied as parameters</returns>
private static bool VerifyVersionGreaterEqual(int majorVersion, int minorVersion)
{
ulong condition = 0;
var osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)),
dwMajorVersion = majorVersion,
dwMinorVersion = minorVersion
};
condition = VerSetConditionMask(condition, VER_MAJORVERSION, VER_GREATER_EQUAL);
condition = VerSetConditionMask(condition, VER_MINORVERSION, VER_GREATER_EQUAL);
return VerifyVersionInfo(ref osVersionInfo, VER_MAJORVERSION | VER_MINORVERSION, condition);
}
/// <summary>
/// Checks OS for required service pack and build version
/// </summary>
/// <param name="majorVersion">Major OS version</param>
/// <param name="minorVersion">Minor OS version</param>
/// <param name="buildVersion">OS Build Version</param>
/// <param name="productType">OS Product Type</param>
/// <param name="servicePack">Minimum Major Service PackVersion</param>
/// <returns>True if Major / Minor OS versions match and service pack / build version are >= parameters</returns>
private static bool VerifyDesktopOSMinRequirement(int majorVersion, int minorVersion, int buildVersion, byte productType, short servicePack)
{
ulong condition = 0;
var osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)),
dwMajorVersion = majorVersion,
dwMinorVersion = minorVersion,
dwBuildNumber = buildVersion,
wProductType = productType,
wServicePackMajor = servicePack
};
condition = VerSetConditionMask(condition, VER_MAJORVERSION, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_MINORVERSION, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_PRODUCT_TYPE, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
condition = VerSetConditionMask(condition, VER_BUILDVERSION, VER_GREATER_EQUAL);
return VerifyVersionInfo(ref osVersionInfo, VER_MAJORVERSION | VER_MINORVERSION | VER_PRODUCT_TYPE |
VER_SERVICEPACKMAJOR | VER_BUILDVERSION, condition);
}
/// <summary>
/// Checks whether the OS version reported via GetVersionEx matches that of VerifyVersionInfo
/// When running in compatibility mode GetVersionEx can return the value of the
/// compatibility setting rather than the actual OS
/// </summary>
/// <param name="majorVersion">Reported OS Major Version</param>
/// <param name="minorVersion">Reported OS Minor Version</param>
/// <param name="buildVersion">Reported OS Build Version</param>
/// <param name="productType">Reported OS Product Type</param>
/// <param name="servicePack">Reported OS Major Service Pack Version</param>
/// <returns>True if actual OS matches reported one</returns>
private static bool IsOSAsReported(int majorVersion, int minorVersion, int buildVersion, byte productType, short servicePack)
{
ulong condition = 0;
var osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)),
dwMajorVersion = majorVersion,
dwMinorVersion = minorVersion,
dwBuildNumber = buildVersion,
wProductType = productType,
wServicePackMajor = servicePack
};
condition = VerSetConditionMask(condition, VER_MAJORVERSION, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_MINORVERSION, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_PRODUCT_TYPE, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_SERVICEPACKMAJOR, VER_EQUAL);
condition = VerSetConditionMask(condition, VER_BUILDVERSION, VER_EQUAL);
return VerifyVersionInfo(ref osVersionInfo, VER_MAJORVERSION | VER_MINORVERSION | VER_PRODUCT_TYPE |
VER_SERVICEPACKMAJOR | VER_BUILDVERSION, condition);
}
/// <summary>
/// Identifies if OS is a Windows Server OS
/// </summary>
/// <returns>True if OS is a Windows Server OS</returns>
private static bool IsServer()
{
ulong condition = 0;
var osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)),
wProductType = NT_WORKSTATION // note the check is that this is not equal as per MS documenation
};
condition = VerSetConditionMask(condition, VER_PRODUCT_TYPE, VER_EQUAL);
return !VerifyVersionInfo(ref osVersionInfo, VER_PRODUCT_TYPE, condition);
}
#endregion
}
}