OSDN Git Service

Windows 8.1/10 version support
[applistation/AppliStation.git] / na-get-lib / NaGet.InteropServices / WindowsVersion.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Runtime.InteropServices.ComTypes;
4
5 namespace NaGet.InteropServices
6 {
7         /// <summary>
8         /// Windows Os Version
9         /// </summary>
10         public sealed class WindowsVersion
11         {
12                 [DllImport("ntdll.dll", SetLastError=true)]
13                 private static extern int RtlGetVersion([In, Out] ref OSVERSIONINFOEX lpVersionInformation);
14                 
15                 [StructLayout(LayoutKind.Sequential)]
16                 private struct OSVERSIONINFOEX
17                 {
18                         public uint dwOSVersionInfoSize;
19                         public uint dwMajorVersion;
20                         public uint dwMinorVersion;
21                         public uint dwBuildNumber;
22                         public uint dwPlatformId;
23                         [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
24                         public string szCSDVersion;
25                         public ushort wServicePackMajor;
26                         public ushort wServicePackMinor;
27                         public ushort wSuiteMask;
28                         public char wProductType;
29                         public char wReserved;
30                 }
31                 
32                 public struct VersionInfo {
33                         public uint MajorVersion;
34                         public uint MinorVersion;
35                         public uint BuildNumber;
36                         public ushort ServicePackMajor;
37                         public ushort ServicePackMinor;
38                 }
39                 
40                 public static VersionInfo? GetVersionInfo() {
41                         VersionInfo? retval = null;
42                         
43                         try {
44                                 OSVERSIONINFOEX versionInformation = new OSVERSIONINFOEX();
45                                 versionInformation.dwOSVersionInfoSize = (uint)Marshal.SizeOf(versionInformation);
46                                 
47                                 int result = RtlGetVersion(ref versionInformation);
48                                 if (result == 0) {
49                                         VersionInfo info = new VersionInfo();
50                                         info.MajorVersion = versionInformation.dwMajorVersion;
51                                         info.MinorVersion = versionInformation.dwMinorVersion;
52                                         info.BuildNumber = versionInformation.dwBuildNumber;
53                                         info.ServicePackMajor = versionInformation.wServicePackMajor;
54                                         info.ServicePackMinor = versionInformation.wServicePackMinor;
55                                         
56                                         retval = info;
57                                 }       
58                         } catch (Exception) {
59                                 // Drop any exception
60                         }
61                         
62                         return retval;
63                 }
64         }
65 }