John Smith
John Smith

Reputation: 505

Getting Service pack major version

In Environment.OSVersion.ServicePack I see just the ServicePack variable, but no Major or minor versions, how do I get Major or minor versions?

Upvotes: 1

Views: 1185

Answers (2)

Roland Pihlakas
Roland Pihlakas

Reputation: 4573

You can use the following code.

    void Main()
    {
        GetServicePackVersion().Major.Dump();
    }

    [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([In, Out] ref OSVERSIONINFOEX osVersionInfo);

    public static Version GetServicePackVersion()
    {
        OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
        osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));

        if (GetVersionEx(ref osVersionInfo))
        {
            Version result = new Version(osVersionInfo.wServicePackMajor, osVersionInfo.wServicePackMinor);
            return result;
        }
        else
        {
            return null;
        }
    }

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Service pack hasn't major or minor version. You must explore Environment.OSVersion.Version to get necessary version information.

Upvotes: -1

Related Questions