NUnit.Framework.Internal.OSPlatform.GetWindows81PlusVersion C# (CSharp) Method

GetWindows81PlusVersion() private static method

Gets the actual OS Version, not the incorrect value that might be returned for Win 8.1 and Win 10
If an application is not manifested as Windows 8.1 or Windows 10, the version returned from Environment.OSVersion will not be 6.3 and 10.0 respectively, but will be 6.2 and 6.3. The correct value can be found in the registry.
private static GetWindows81PlusVersion ( System.Version version ) : System.Version
version System.Version The original version
return System.Version
        private static Version GetWindows81PlusVersion(Version version)
        {
            try
            {
                using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
                {
                    if (key != null)
                    {
                        var buildStr = key.GetValue("CurrentBuildNumber") as string;
                        int build = 0;
                        int.TryParse(buildStr, out build);

                        // These two keys are in Windows 10 only and are DWORDS
                        var major = key.GetValue("CurrentMajorVersionNumber") as int?;
                        var minor = key.GetValue("CurrentMinorVersionNumber") as int?;
                        if (major.HasValue && minor.HasValue)
                        {
                            return new Version(major.Value, minor.Value, build);
                        }

                        // If we get here, we are not Windows 10, so we are Windows 8
                        // or 8.1. 8.1 might report itself as 6.2, but will have 6.3
                        // in the registry. We can't do this earlier because for backwards
                        // compatibility, Windows 10 also has 6.3 for this key.
                        var currentVersion = key.GetValue("CurrentVersion") as string;
                        if(currentVersion == "6.3")
                        {
                            return new Version(6, 3, build);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return version;
        }
        #endregion