/
OsVersionInfo.cs
2031 lines (1896 loc) · 93.6 KB
/
OsVersionInfo.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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Runtime.InteropServices;
using System.Reflection;
namespace SystemInfo
{
#region public types
//From System.Runtime.InteropServices.Architecture in System.Runtime.InteropServices.RuntimeInformation.dll (System.Runtime.InteropServices.RuntimeInformation package)
public struct OSPlatform : IEquatable<OSPlatform>
{
private readonly string m_OSPlatform;
private OSPlatform(string osPlatform)
{
if (osPlatform == null)
{
throw new ArgumentNullException(nameof(osPlatform));
}
m_OSPlatform = osPlatform;
}
public static OSPlatform Windows
{
get { return new OSPlatform("WINDOWS"); }
}
public bool Equals(OSPlatform other)
{
return Equals(other.m_OSPlatform);
}
internal bool Equals(string other)
{
return string.Equals(m_OSPlatform, other, StringComparison.Ordinal);
}
public override bool Equals(object obj)
{
return obj is OSPlatform && Equals((OSPlatform)obj);
}
public override int GetHashCode()
{
return m_OSPlatform == null ? 0 : m_OSPlatform.GetHashCode();
}
public override string ToString()
{
return m_OSPlatform ?? string.Empty;
}
public static bool operator ==(OSPlatform left, OSPlatform right)
{
return left.Equals(right);
}
public static bool operator !=(OSPlatform left, OSPlatform right)
{
return !(left == right);
}
}
/// <summary>
/// Identifies platform of windows operating system
/// </summary>
[Serializable]
internal enum OSWindowsID
{
/// <summary>
/// Unknown OS
/// </summary>
Unknown = 0,
/// <summary>
/// Windows 2000 (v. 5.0)
/// </summary>
/// <remarks>
/// Support up to FW 2.0
/// </remarks>
Windows2K = 4,
/// <summary>
/// Windows XP or Windows Server 2003 (v. 5.1, 5.2)
/// </summary>
/// <remarks>
/// Support up to FW 4.0
/// </remarks>
WindowsXP_2K3 = 5,
/// <summary>
/// Windows Vista or Windows Server 2008 (v. 6.0)
/// </summary>
WindowsVista_2K8 = 6,
/// <summary>
/// Windows 7 or Windows Server 2008 R2 (v. 6.1)
/// </summary>
Windows7_2K8R2 = 7,
/// <summary>
/// Windows 8 or Windows Server 2012 (v. 6.2)
/// </summary>
Windows8_2K12 = 8,
/// <summary>
/// Windows 10, Windows Server 10
/// </summary>
Windows10 = 12
}
/// <summary>
/// Identifies the product type of operating system.
/// </summary>
[Serializable]
internal enum ProductTypeID
{
/// <summary>
/// Workstation (Client) operating system
/// </summary>
Workstation = 0,
/// <summary>
/// Server operating system
/// </summary>
Server = 1
}
//From System.Runtime.InteropServices.Architecture in System.Runtime.InteropServices.RuntimeInformation.dll (System.Runtime.InteropServices.RuntimeInformation package)
[Serializable]
public enum Architecture
{
X86 = 0,
X64 = 1,
Arm = 2,
Arm64 = 3
}
#endregion
/// <summary>
/// Class gets information about platform, edition, version of operating system that are currently installed
/// and gets concatenated string with description of operating system.
/// </summary>
/// <remarks>
/// Supports .NET Framework 4.0 or later
/// </remarks>
internal sealed class OsVersionInfo
{
#region Win32 API classes
#region OSVERSIONINFOEX struct
/// <summary>
/// OSVERSIONINFOEX struct contains operating system version information.
/// The information includes major and minor version numbers, a build number, a platform identifier,
/// and information about product suites and the latest Service Pack installed on the system.
/// This structure is used with the <c>GetVersionEx</c> function.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct OSVERSIONINFOEX
{
/// <summary>
/// dwOSVersionInfoSize
/// </summary>
public int dwOSVersionInfoSize;
/// <summary>
/// dwMajorVersion
/// </summary>
public int dwMajorVersion;
/// <summary>
/// dwMinorVersion
/// </summary>
public int dwMinorVersion;
/// <summary>
/// dwBuildNumber
/// </summary>
public int dwBuildNumber;
/// <summary>
/// dwPlatformId
/// </summary>
public int dwPlatformId;
/// <summary>
/// szCSDVersion
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
/// <summary>
/// wServicePackMajor
/// </summary>
public short wServicePackMajor;
/// <summary>
/// wServicePackMinor
/// </summary>
public short wServicePackMinor;
/// <summary>
/// wSuiteMask
/// </summary>
public ushort wSuiteMask;
/// <summary>
/// wProductType
/// </summary>
public byte wProductType;
/// <summary>
/// wReserved
/// </summary>
public byte wReserved;
}
#endregion
#region OSVERSIONINFO struct
/// <summary>
/// OSVERSIONINFO struct contains operating system version information.
/// The information includes major and minor version numbers, a build number, a platform identifier,
/// and descriptive text about the operating system.
/// This structure is used with the <c>GetVersionEx</c> function.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct OSVERSIONINFO
{
/// <summary>
/// dwOSVersionInfoSize
/// </summary>
public int dwOSVersionInfoSize;
/// <summary>
/// dwMajorVersion
/// </summary>
public int dwMajorVersion;
/// <summary>
/// dwMinorVersion
/// </summary>
public int dwMinorVersion;
/// <summary>
/// dwBuildNumber
/// </summary>
public int dwBuildNumber;
/// <summary>
/// dwPlatformId
/// </summary>
public int dwPlatformId;
/// <summary>
/// szCSDVersion
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
}
#endregion
#region SYSTEM_INFO struct
/// <summary>
/// SYSTEM_INFO struct contains information about the current computer system.
/// This includes the architecture and type of the processor, the number of processors
/// in the system, the page size, and other such information.
/// This structure is used with the <c>GetSystemInfo</c> and <c>GetNativeSystemInfo</c> functions.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEM_INFO
{
/// <summary>
/// System's processor architecture. This value can be one of the following values:
/// PROCESSOR_ARCHITECTURE_UNKNOWN, PROCESSOR_ARCHITECTURE_INTEL, PROCESSOR_ARCHITECTURE_IA64, PROCESSOR_ARCHITECTURE_AMD64
/// </summary>
public short wProcessorArchitecture;
/// <summary>
/// Reserved for future use.
/// </summary>
public short wReserved;
/// <summary>
/// Page size and the granularity of page protection and commitment (VirtualAlloc).
/// </summary>
public int dwPageSize;
/// <summary>
/// Pointer to the lowest memory address accessible to applications and dynamic-link libraries (DLLs).
/// </summary>
public IntPtr lpMinimumApplicationAddress;
/// <summary>
/// Pointer to the highest memory address accessible to applications and DLLs.
/// </summary>
public IntPtr lpMaximumApplicationAddress;
/// <summary>
/// Mask representing the set of processors configured into the system. Bit 0 is processor 0; bit 31 is processor 31.
/// </summary>
public IntPtr dwActiveProcessorMask;
/// <summary>
/// Number of processors in the system
/// </summary>
public int dwNumberOfProcessors;
/// <summary>
/// An obsolete member that is retained for compatibility with Windows NT 3.5 and Windows Me/98/95.
/// Use the wProcessorArchitecture, wProcessorLevel, and wProcessorRevision members to determine the type of processor.
/// Specifies the type of processor in the system: 386, 486, 586
/// </summary>
public int dwProcessorType;
/// <summary>
/// Granularity for the starting address at which virtual memory can be allocated. For example, a VirtualAlloc request to allocate 1 byte will reserve an address space of dwAllocationGranularity bytes.
/// </summary>
public int dwAllocationGranularity;
/// <summary>
/// System's architecture-dependent processor level. It should be used only for display purposes.
/// </summary>
/// <remarks>
/// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_INTEL
/// wProcessorLevel is defined by the CPU vendor
/// (following values: 3 - Intel 80386, 4 - Intel 80486, 5 - Intel Pentium, 6 - Intel Pentium Pro, Pentium II or Pentium III, 15 - Pentium 4)
///
/// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_IA64
/// wProcessorLevel is set to 1.
///
/// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_MIPS or PROCESSOR_ARCHITECTURE_ALPHA or PROCESSOR_ARCHITECTURE_PPC
/// wProcessorLevel is processor version number
///
/// </remarks>
public short wProcessorLevel;
/// <summary>
/// Architecture-dependent processor revision
/// </summary>
/// <remarks>
/// The following table shows how the revision value is assembled for each type of processor architecture.
///
/// Processor | Value
/// ----------------------------------------------------------------------------------------------
/// Intel Pentium, Cyrix, or | The high byte is the model and the low byte is the stepping. For example,
/// NextGen 586 | if the value is xxyy, the model number and stepping can be displayed
/// } as follows: Model xx, Stepping yy
/// ----------------------------------------------------------------------------------------------
/// Intel 80386 or 80486 | A value of the form xxyz.
/// | If xx is equal to 0xFF, y - 0xA is the model number, and z is the stepping
/// | identifier. For example, an Intel 80486-D0 system returns 0xFFD0
/// |
/// | If xx is not equal to 0xFF, xx + 'A' is the stepping letter and yz is the
/// | minor stepping.
/// ----------------------------------------------------------------------------------------------
/// </remarks>
public ushort wProcessorRevision;
}
#endregion
#region constants
//wSuiteMask of OSVERSIONINFOEX struct consts
private const ushort VER_SUITE_SMALLBUSINESS = 0x0001; //Microsoft Small Business Server was once installed on the system, but may have been upgraded to another version of Windows. Refer to the Remarks section for more information about this bit flag.
private const ushort VER_SUITE_ENTERPRISE = 0x0002; //Windows Server 2008 Enterprise, Windows Server 2003, Enterprise Edition, or Windows 2000 Advanced Server is installed. Refer to the Remarks section for more information about this bit flag.
private const ushort VER_SUITE_BACKOFFICE = 0x0004; //Microsoft BackOffice components are installed.
private const ushort VER_SUITE_TERMINAL = 0x0010; //Terminal Services is installed. This value is always set.
private const ushort VER_SUITE_SMALLBUSINESS_RESTRICTED = 0x0020; //Microsoft Small Business Server is installed with the restrictive client license in force. Refer to the Remarks section for more information about this bit flag.
private const ushort VER_SUITE_EMBEDDEDNT = 0x0040; //Windows XP Embedded is installed.
private const ushort VER_SUITE_DATACENTER = 0x0080; //Windows Server 2008 Datacenter, Windows Server 2003, Datacenter Edition, or Windows 2000 Datacenter Server is installed.
private const ushort VER_SUITE_SINGLEUSERTS = 0x0100; //Remote Desktop is supported, but only one interactive session is supported. This value is set unless the system is running in application server mode.
private const ushort VER_SUITE_PERSONAL = 0x0200; //Windows Vista Home Premium, Windows Vista Home Basic, or Windows XP Home Edition is installed.
private const ushort VER_SUITE_BLADE = 0x0400; //Windows Server 2003, Web Edition is installed
private const ushort VER_SUITE_STORAGE_SERVER = 0x2000; //Windows Storage Server 2003 R2 or Windows Storage Server 2003is installed.
private const ushort VER_SUITE_COMPUTE_SERVER = 0x4000; //Windows Server 2003, Compute Cluster Edition is installed.
private const ushort VER_SUITE_WH_SERVER = 0x8000; //Windows Home Server is installed.
//wProductType of OSVERSIONINFOEX struct consts
private const byte VER_NT_WORKSTATION = 1;
private const byte VER_NT_DOMAIN_CONTROLLER = 2;
private const byte VER_NT_SERVER = 3;
//GetProductInfo ReturnedProductType consts
private const uint PRODUCT_UNDEFINED = 0x00000000; //An unknown product
private const uint PRODUCT_ULTIMATE = 0x00000001; //Ultimate Edition
private const uint PRODUCT_HOME_BASIC = 0x00000002; //Home Basic Edition
private const uint PRODUCT_HOME_PREMIUM = 0x00000003; //Home Premium Edition
private const uint PRODUCT_ENTERPRISE = 0x00000004; //Enterprise Edition
private const uint PRODUCT_HOME_BASIC_N = 0x00000005; //Home Basic Edition
private const uint PRODUCT_BUSINESS = 0x00000006; //Business Edition
private const uint PRODUCT_STANDARD_SERVER = 0x00000007; //Server Standard Edition (full installation)
private const uint PRODUCT_DATACENTER_SERVER = 0x00000008; //Server Datacenter Edition (full installation)
private const uint PRODUCT_SMALLBUSINESS_SERVER = 0x00000009; //Small Business Server
private const uint PRODUCT_ENTERPRISE_SERVER = 0x0000000A; //Server Enterprise Edition (full installation)
private const uint PRODUCT_STARTER = 0x0000000B; //Starter Edition
private const uint PRODUCT_DATACENTER_SERVER_CORE = 0x0000000C; //Server Datacenter Edition (core installation)
private const uint PRODUCT_STANDARD_SERVER_CORE = 0x0000000D; //Server Standard Edition (core installation)
private const uint PRODUCT_ENTERPRISE_SERVER_CORE = 0x0000000E; //Server Enterprise Edition (core installation)
private const uint PRODUCT_ENTERPRISE_SERVER_IA64 = 0x0000000F; //Server Enterprise Edition for Itanium-based Systems
private const uint PRODUCT_BUSINESS_N = 0x00000010; //Business Edition
private const uint PRODUCT_WEB_SERVER = 0x00000011; //Web Server Edition (full installation)
private const uint PRODUCT_CLUSTER_SERVER = 0x00000012; //Cluster Server Edition
private const uint PRODUCT_HOME_SERVER = 0x00000013; //Home Server Edition
private const uint PRODUCT_STORAGE_EXPRESS_SERVER = 0x00000014; //Storage Server Express Edition
private const uint PRODUCT_STORAGE_STANDARD_SERVER = 0x00000015; //Storage Server Standard Edition
private const uint PRODUCT_STORAGE_WORKGROUP_SERVER = 0x00000016; //Storage Server Workgroup Edition
private const uint PRODUCT_STORAGE_ENTERPRISE_SERVER = 0x00000017; //Storage Server Enterprise Edition
private const uint PRODUCT_SERVER_FOR_SMALLBUSINESS = 0x00000018; //Server for Small Business Edition
private const uint PRODUCT_SMALLBUSINESS_SERVER_PREMIUM = 0x00000019; //Small Business Server Premium Edition
private const uint PRODUCT_HOME_PREMIUM_N = 0x0000001A; //Home Premium Edition
private const uint PRODUCT_ENTERPRISE_N = 0x0000001B; //Enterprise Edition
private const uint PRODUCT_ULTIMATE_N = 0x0000001C; //Ultimate Edition
private const uint PRODUCT_WEB_SERVER_CORE = 0x0000001D; //Web Server Edition (core installation)
private const uint PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT = 0x0000001E; //Windows Essential Business Server Management Server
private const uint PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY = 0x0000001F; //Windows Essential Business Server Security Server
private const uint PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING = 0x00000020; //Windows Essential Business Server Messaging Server
private const uint PRODUCT_SERVER_FOUNDATION = 0x00000021; //Server Foundation
private const uint PRODUCT_HOME_PREMIUM_SERVER = 0x00000022; //Windows Home Server 2011
private const uint PRODUCT_SERVER_FOR_SMALLBUSINESS_V = 0x00000023; //Windows Server 2008 without Hyper-V for Windows Essential Server Solutions
private const uint PRODUCT_STANDARD_SERVER_V = 0x00000024; //Server Standard Edition without Hyper-V (full installation)
private const uint PRODUCT_DATACENTER_SERVER_V = 0x00000025; //Server Datacenter Edition without Hyper-V (full installation)
private const uint PRODUCT_ENTERPRISE_SERVER_V = 0x00000026; //Server Enterprise Edition without Hyper-V (full installation)
private const uint PRODUCT_DATACENTER_SERVER_CORE_V = 0x00000027; //Server Datacenter Edition without Hyper-V (core installation)
private const uint PRODUCT_STANDARD_SERVER_CORE_V = 0x00000028; //Server Standard Edition without Hyper-V (core installation)
private const uint PRODUCT_ENTERPRISE_SERVER_CORE_V = 0x00000029; //Server Enterprise Edition without Hyper-V (core installation)
private const uint PRODUCT_HYPERV = 0x0000002A; //Microsoft Hyper-V Server
private const uint PRODUCT_STORAGE_EXPRESS_SERVER_CORE = 0x0000002B; //Storage Server Express (core installation)
private const uint PRODUCT_STORAGE_STANDARD_SERVER_CORE = 0x0000002C; //Storage Server Standard (core installation)
private const uint PRODUCT_STORAGE_WORKGROUP_SERVER_CORE = 0x0000002D; //Storage Server Workgroup (core installation)
private const uint PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE = 0x0000002E; //Storage Server Enterprise (core installation)
private const uint PRODUCT_STARTER_N = 0x0000002F; //Starter N
private const uint PRODUCT_PROFESSIONAL = 0x00000030; //Professional
private const uint PRODUCT_PROFESSIONAL_N = 0x00000031; //Professional N
private const uint PRODUCT_SB_SOLUTION_SERVER = 0x00000032; //Windows Small Business Server 2011 Essentials
private const uint PRODUCT_SERVER_FOR_SB_SOLUTIONS = 0x00000033; //Server For SB Solutions
private const uint PRODUCT_STANDARD_SERVER_SOLUTIONS = 0x00000034; //Server Solutions Premium
private const uint PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE = 0x00000035; //Server Solutions Premium (core installation)
private const uint PRODUCT_SB_SOLUTION_SERVER_EM = 0x00000036; //Server For SB Solutions EM
private const uint PRODUCT_SERVER_FOR_SB_SOLUTIONS_EM = 0x00000037; //Server For SB Solutions EM
private const uint PRODUCT_SOLUTION_EMBEDDEDSERVER = 0x00000038; //Windows MultiPoint Server
private const uint PRODUCT_ESSENTIALBUSINESS_SERVER_MGMT = 0x0000003B; //Windows Essential Server Solution Management
private const uint PRODUCT_ESSENTIALBUSINESS_SERVER_ADDL = 0x0000003C; //Windows Essential Server Solution Additional
private const uint PRODUCT_ESSENTIALBUSINESS_SERVER_MGMTSVC = 0x0000003D; //Windows Essential Server Solution Management SVC
private const uint PRODUCT_ESSENTIALBUSINESS_SERVER_ADDLSVC = 0x0000003E; //Windows Essential Server Solution Additional SVC
private const uint PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE = 0x0000003F; //Small Business Server Premium (core installation)
private const uint PRODUCT_CLUSTER_SERVER_V = 0x00000040; //Server Hyper Core V
private const uint PRODUCT_STARTER_E = 0x00000042; //Starter E
private const uint PRODUCT_HOME_BASIC_E = 0x00000043; //Home Basic E
private const uint PRODUCT_HOME_PREMIUM_E = 0x00000044; //Home Premium E
private const uint PRODUCT_PROFESSIONAL_E = 0x00000045; //Professional E
private const uint PRODUCT_ENTERPRISE_E = 0x00000046; //Enterprise E
private const uint PRODUCT_ULTIMATE_E = 0x00000047; //Ultimate E
private const uint PRODUCT_ENTERPRISE_EVALUATION = 0x00000048; //Server Enterprise (evaluation installation)
private const uint PRODUCT_DEVELOPER_PREVIEW = 0x0000004A; //Windows 8 Developer Preview
private const uint PRODUCT_MULTIPOINT_STANDARD_SERVER = 0x0000004C; //Windows MultiPoint Server Standard (full installation)
private const uint PRODUCT_MULTIPOINT_PREMIUM_SERVER = 0x0000004D; //Windows MultiPoint Server Premium (full installation)
private const uint PRODUCT_STANDARD_EVALUATION_SERVER = 0x0000004F; //Server Standard (evaluation installation)
private const uint PRODUCT_DATACENTER_EVALUATION_SERVER = 0x00000050; //Server Datacenter (evaluation installation)
private const uint PRODUCT_ENTERPRISE_N_EVALUATION = 0x00000054; //Enterprise N (evaluation installation)
private const uint PRODUCT_STORAGE_WORKGROUP_EVALUATION_SERVER = 0x0000005F; //Storage Server Workgroup (evaluation installation)
private const uint PRODUCT_STORAGE_STANDARD_EVALUATION_SERVER = 0x00000060; //Storage Server Standard (evaluation installation)
private const uint PRODUCT_CORE_N = 0x00000062; //Windows 8 N
private const uint PRODUCT_CORE_COUNTRYSPECIFIC = 0x00000063; //Windows 8 China
private const uint PRODUCT_CORE_SINGLELANGUAGE = 0x00000064; //Windows 8 Single Language
private const uint PRODUCT_CORE = 0x00000065; //Windows 8
private const uint PRODUCT_PROFESSIONAL_WMC = 0x00000067; //Professional with Media Center
private const uint PRODUCT_UNLICENSED = 0xABCDABCD; //Product has not been activated and is no longer in the grace period
//GetSystemMetrics SO SpecialEdition info consts
private const int SM_TABLETPC = 86; //Windows XP Tablet PC edition
private const int SM_MEDIACENTER = 87; //Windows XP, Media Center Edition
private const int SM_STARTER = 88; //Windows Server 2003 R2 version
private const int SM_SERVERR2 = 89; //Windows XP Starter Edition or Windows Vista Starter Edition
// ProcessorArchitecture of SYSTEM_INFO struct consts
private const short PROCESSOR_ARCHITECTURE_UNKNOWN = -1;
private const short PROCESSOR_ARCHITECTURE_INTEL = 0;
private const short PROCESSOR_ARCHITECTURE_MIPS = 1;
private const short PROCESSOR_ARCHITECTURE_ALPHA = 2;
private const short PROCESSOR_ARCHITECTURE_PPC = 3;
private const short PROCESSOR_ARCHITECTURE_SHX = 4;
private const short PROCESSOR_ARCHITECTURE_ARM = 5;
private const short PROCESSOR_ARCHITECTURE_IA64 = 6;
private const short PROCESSOR_ARCHITECTURE_ALPHA64 = 7;
private const short PROCESSOR_ARCHITECTURE_MSIL = 8;
private const short PROCESSOR_ARCHITECTURE_AMD64 = 9;
private const short PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 = 10;
private const short PROCESSOR_ARCHITECTURE_ARM64 = 12;
#endregion
#region SafeNativeMethods class
[System.Security.SuppressUnmanagedCodeSecurity]
private static class SafeNativeMethods
{
[DllImport("ntdll.dll")]
internal static extern int RtlGetVersion(ref OSVERSIONINFOEX lpVersionInformation);
/// <summary>
/// Retrieves information about the current operating system.
/// </summary>
[DllImport("kernel32", CharSet = CharSet.Auto)]
internal static extern int GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
/// <summary>
/// Retrieves the product type for the operating system on the local computer, and maps the type to the product types supported by the specified operating system.
/// </summary>
[DllImport("Kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetProductInfo(int dwOSMajorVersion, int dwOSMinorVersion, int dwSpMajorVersion, int dwSpMinorVersion, out uint pdwReturnedProductType);
/// <summary>
/// Retrieves information about the current system.
/// </summary>
[DllImport("kernel32.dll", EntryPoint = "GetSystemInfo")]
internal static extern void GetSystemInfo(ref SYSTEM_INFO sysinfo);
/// <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>
[DllImport("kernel32.dll", EntryPoint = "GetNativeSystemInfo")]
internal static extern void GetNativeSystemInfo(ref SYSTEM_INFO sysinfo);
/// <summary>
/// Retrieves the specified system metric or system configuration setting.
/// </summary>
[DllImport("user32.dll", EntryPoint = ("GetSystemMetrics"))]
internal static extern int GetSystemMetrics(int nIndex);
/// <summary>
/// Retrieves a module handle for the specified module. The module must have been loaded by the calling process.
/// </summary>
[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true, SetLastError = true)]
internal static extern IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPStr)] string lpModuleName);
/// <summary>
/// Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).
/// </summary>
[DllImport("kernel32.dll", BestFitMapping = false, ThrowOnUnmappableChar = true)]
internal static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string lpProcName);
}
#endregion
#endregion
#region member varible and default property initialization
private short m_ProcessorArchitecture;
private OSVERSIONINFOEX m_WindowsOSVersionInfo;
private uint m_WindowsProductInfoProductType;
private string m_WindowsServicePack;
private int m_WindowsCSDBuildNumber;
private OSPlatform m_OSPlatform;
private string m_OSName;
private string m_OSFullName;
private OSWindowsID m_OSWindows;
private string m_NETFrameworkVersionString;
#endregion
#region constructors and destructors
/// <summary>
/// OsVersionInfo constructor loads operation system informations.
/// </summary>
/// <exception cref="NotSupportedException">Cannot get system informations.</exception>
public OsVersionInfo()
{
SetVersionInformation();
}
#endregion
#region action methods
/// <summary>
/// Gets the concatenated string representation of the platform with edition, service pack and build number of operating system that are currently installed.
/// </summary>
/// <returns>concatenated string representation of the platform with edition, service pack and build number of operating system that are currently installed</returns>
public override string ToString()
{
return this.VersionString;
}
/// <summary>
/// Gets the concatenated string representation of the platform with edition, service pack and build number of operating system that are currently installed.
/// </summary>
/// <returns>concatenated string representation of the platform with edition, service pack and build number of operating system that are currently installed</returns>
public static string GetVersionString()
{
return new OsVersionInfo().VersionString;
}
#endregion
#region property getters/setters
#region OS Information
/// <summary>
/// Operating system or platform currently installed.
/// </summary>
public OSPlatform OSPlatform
{
get { return m_OSPlatform; }
}
/// <summary>
/// Gets the string representation of the platform of operating system that are currently installed.
/// </summary>
public string OSName
{
get { return m_OSName; }
}
/// <summary>
/// Gets the string representation of the platform with edition of operating system that are currently installed.
/// </summary>
public string OSFullName
{
get { return m_OSFullName; }
}
/// <summary>
/// Gets the platform identifier of windows operating system that are currently installed.
/// </summary>
public OSWindowsID OSWindows
{
get { return m_OSWindows; }
}
/// <summary>
/// Version of operating system that are currently installed.
/// </summary>
/// <remarks>
/// Revision number of version is not set, use only major, minor a build numbers
/// </remarks>
public Version OSVersion
{
get
{
if (this.OSPlatform == OSPlatform.Windows)
{
return new Version(m_WindowsOSVersionInfo.dwMajorVersion, m_WindowsOSVersionInfo.dwMinorVersion, m_WindowsOSVersionInfo.dwBuildNumber, 0);
}
return new Version(0, 0, 0);
}
}
/// <summary>
/// Build number of operating system that are currently installed.
/// </summary>
public int OSBuild
{
get { return this.OSVersion.Build; }
}
/// <summary>
/// Operating system architecture version.
/// </summary>
public Architecture OSArchitecture
{
get
{
switch (m_ProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_ARM64:
return Architecture.Arm64;
case PROCESSOR_ARCHITECTURE_ARM:
return Architecture.Arm;
case PROCESSOR_ARCHITECTURE_AMD64:
case PROCESSOR_ARCHITECTURE_IA64:
return Architecture.X64;
case PROCESSOR_ARCHITECTURE_INTEL:
return Architecture.X86;
}
return Architecture.X86;
}
}
/// <summary>
/// Product type of operating system that are currently installed.
/// </summary>
public ProductTypeID WindowsProductType
{
get
{
if (m_WindowsOSVersionInfo.wProductType == VER_NT_SERVER ||
m_WindowsOSVersionInfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
{
return ProductTypeID.Server;
}
return ProductTypeID.Workstation;
}
}
/// <summary>
/// Service pack that are currently installed on the operating system.
/// </summary>
public string WindowsServicePack
{
get { return m_WindowsServicePack; }
}
/// <summary>
/// Version of service pack that are currently installed on the operating system.
/// </summary>
public Version WindowsServicePackVersion
{
get
{
return new Version(m_WindowsOSVersionInfo.wServicePackMajor, m_WindowsOSVersionInfo.wServicePackMinor);
}
}
/// <summary>
/// Service pack CSDBuildNumber on the operating system.
/// </summary>
public int WindowsCSDBuildNumber
{
get { return m_WindowsCSDBuildNumber; }
}
/// <summary>
/// BuildLab version string of operating system that are currently installed.
/// </summary>
public string WindowsBuildLab
{
get
{
if (this.OSPlatform != OSPlatform.Windows)
{
return null;
}
if (Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion") == null)
{
return null;
}
string buildLab = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("BuildLabEx") as string;
if (buildLab == null)
{
buildLab = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("BuildLab") as string;
}
return buildLab;
}
}
/// <summary>
/// Gets the concatenated string representation of the platform with edition, service pack and build number of operating system that are currently installed
/// </summary>
public string VersionString
{
get
{
if (this.OSPlatform == OSPlatform.Windows)
{
//Windows 2000, Windows XP, Windows Vista or later
if (string.IsNullOrEmpty(m_WindowsServicePack))
{
return m_OSFullName + " (Build " + m_WindowsOSVersionInfo.dwBuildNumber.ToString(System.Globalization.CultureInfo.CurrentCulture) + ")";
}
return m_OSFullName + " " + m_WindowsServicePack + " (Build " + m_WindowsOSVersionInfo.dwBuildNumber.ToString(System.Globalization.CultureInfo.CurrentCulture) + ")";
}
return m_OSFullName;
}
}
/// <summary>
/// Core installation of Windows Server 2008 or later operating system
/// </summary>
public bool WindowsCoreInstallation
{
get
{
switch (m_WindowsProductInfoProductType)
{
case PRODUCT_WEB_SERVER_CORE:
case PRODUCT_STANDARD_SERVER_CORE:
case PRODUCT_DATACENTER_SERVER_CORE:
case PRODUCT_ENTERPRISE_SERVER_CORE:
case PRODUCT_STANDARD_SERVER_CORE_V:
case PRODUCT_DATACENTER_SERVER_CORE_V:
case PRODUCT_ENTERPRISE_SERVER_CORE_V:
case PRODUCT_HYPERV:
case PRODUCT_STORAGE_EXPRESS_SERVER_CORE:
case PRODUCT_STORAGE_STANDARD_SERVER_CORE:
case PRODUCT_STORAGE_WORKGROUP_SERVER_CORE:
case PRODUCT_STORAGE_ENTERPRISE_SERVER_CORE:
case PRODUCT_STANDARD_SERVER_SOLUTIONS_CORE:
case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM_CORE:
return true;
}
return false;
}
}
#endregion
#region CLR information
public string Runtime
{
get { return "CLR"; }
}
public string RuntimeDisplayName
{
get { return ".NET Framework"; }
}
/// <summary>
/// Version (the major, minor, build, and revision numbers) of the common language runtime.
/// </summary>
public Version CLRVersion
{
get { return System.Environment.Version; }
}
public string RuntimeFramework
{
get
{
return ".NETFramework,Version=v" + NETFrameworkVersionString;
}
}
/// <summary>
/// Version of .NET framework that are currently installed, e.g.: "4.5".
/// </summary>
public string NETFrameworkVersionString
{
get
{
if (m_NETFrameworkVersionString == null)
{
m_NETFrameworkVersionString = GetNETFrameworkVersionString();
}
return m_NETFrameworkVersionString;
}
}
#endregion
#endregion
#region private member functions
/// <summary>
/// Set OS version information to m_OSName, m_OSFullName, m_OSWindows
/// </summary>
private void SetVersionInformation()
{
//Get ProcessorArchitecture from SYSTEM_INFO struct
m_ProcessorArchitecture = GetProcessorArchitecture();
m_OSPlatform = OSPlatform.Windows;
//Get OSVERSIONINFOEX struct
m_WindowsOSVersionInfo = GetWindowsOSVersionInfo();
if (m_WindowsOSVersionInfo.dwMajorVersion >= 6)
{
//Get ProductInfo ProductType
uint dwType;
if (SafeNativeMethods.GetProductInfo(m_WindowsOSVersionInfo.dwMajorVersion, m_WindowsOSVersionInfo.dwMinorVersion, 0, 0, out dwType))
{
m_WindowsProductInfoProductType = dwType;
}
}
SetWindowsPlatformInformation();
//Set m_WindowsServicePack and m_WindowsCSDBuildNumber
SetWindowsServicePackString();
}
/// <summary>
/// Get ProcessorArchitecture from SYSTEM_INFO struct
/// </summary>
/// <returns><c>short</c> ProcessorArchitecture</returns>
private static short GetProcessorArchitecture()
{
var si = new SYSTEM_INFO();
if (SafeNativeMethods.GetProcAddress(SafeNativeMethods.GetModuleHandle("KERNEL32.dll"), "GetNativeSystemInfo") != (IntPtr)0)
{
//On Windows XP or later
SafeNativeMethods.GetNativeSystemInfo(ref si);
return si.wProcessorArchitecture;
}
SafeNativeMethods.GetSystemInfo(ref si);
return si.wProcessorArchitecture;
}
/// <summary>
/// Get OSVERSIONINFOEX struct
/// </summary>
/// <returns>OSVERSIONINFOEX struct</returns>
/// <exception cref="NotSupportedException">Cannot get OSVERSIONINFO struct.</exception>
private static OSVERSIONINFOEX GetWindowsOSVersionInfo()
{
var osVersionInfo = new OSVERSIONINFOEX();
//Get Win32 API extended OSVERSIONINFOEX struct
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(new OSVERSIONINFOEX());
if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6)
{
//Try use RtlGetVersion function because GetOSVersionEx API gives wrong result on Windows 8.1 and Windows 10 (without applications manifested compatibility keys)
int rtlResult = SafeNativeMethods.RtlGetVersion(ref osVersionInfo);
if (rtlResult == 0)
{
return osVersionInfo;
}
}
int result = SafeNativeMethods.GetVersionEx(ref osVersionInfo);
if (result == 0)
{
//Get only OSVERSIONINFO struct
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(new OSVERSIONINFO());
result = SafeNativeMethods.GetVersionEx(ref osVersionInfo);
if (result == 0)
{
throw new NotSupportedException();
}
}
return osVersionInfo;
}
/// <summary>
/// Set Windows Platform information to m_OSName, m_OSFullName, m_OSWindows
/// </summary>
private void SetWindowsPlatformInformation()
{
m_OSPlatform = OSPlatform.Windows;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetWindowsInformation();
}
else if (Environment.OSVersion.Platform == PlatformID.WinCE) //Specifies the Windows CE OS.
{
m_OSName = "Windows CE";
m_OSFullName = "Windows CE";
}
else
{
m_OSName = Environment.OSVersion.Platform.ToString();
m_OSFullName = Environment.OSVersion.Platform.ToString();
}
}
/// <summary>
/// Windows Information - Windows 2000, Windows XP, Windows Vista, Windows 7 and later
/// </summary>
private void SetWindowsInformation()
{
switch (m_WindowsOSVersionInfo.dwMajorVersion)
{
case 5:
if (m_WindowsOSVersionInfo.dwMinorVersion == 0)
{
SetWin2KInformation(); //Windows 2000
}
else if (m_WindowsOSVersionInfo.dwMinorVersion == 1)
{
SetWinXPInformation(); //Windows XP
}
else
{
SetWin2K3Information(); //Windows Server 2003 or Windows XP Professional x64 Edition
}
break;
case 6:
if (m_WindowsOSVersionInfo.dwMinorVersion == 0)
{
SetWinVistaWin2K8Information(); //Windows Vista or Windows Server 2008
}
else if (m_WindowsOSVersionInfo.dwMinorVersion == 1)
{
SetWin7Win2K8R2Information(); //Windows 7 or Windows Server 2008 R2
}
else if (m_WindowsOSVersionInfo.dwMinorVersion == 2 || m_WindowsOSVersionInfo.dwMinorVersion == 3)
{
SetWin8Win2012Information(); //Windows 8 or Windows Server 2012
}
else if (m_WindowsOSVersionInfo.dwMinorVersion == 4)
{
SetWin10Information(); //Windows 10, Windows Server 10
}
break;
case 7:
case 8:
case 9:
case 10:
case 11:
SetWin10Information(); //Windows 10, Windows Server 10 (Major 10, Minor 0)
break;
}
}
#region Windows operating system informations medhods
/// <summary>
/// Windows 2000 operating system informations
/// </summary>
private void SetWin2KInformation()
{
m_OSName = "Windows 2000";
m_OSFullName = "Windows 2000";
m_OSWindows = OSWindowsID.Windows2K;
if (m_WindowsOSVersionInfo.wProductType == VER_NT_WORKSTATION)
{
if ((m_WindowsOSVersionInfo.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
{
m_OSFullName += " Personal";
}
else
{
m_OSFullName += " Professional";
}
}
else if (m_WindowsOSVersionInfo.wProductType == VER_NT_SERVER ||
m_WindowsOSVersionInfo.wProductType == VER_NT_DOMAIN_CONTROLLER)
{
if ((m_WindowsOSVersionInfo.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
{
m_OSFullName += " Datacenter Server";
}
else if ((m_WindowsOSVersionInfo.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)