2022-10-04 11:53:07 +08:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
using Microsoft.Win32;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient.WinAPI
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Retrieve information about the current Windows version
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Environment.OSVersion does not work with Windows 10.
|
|
|
|
|
|
/// It returns 6.2 which is Windows 8
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
/// <seealso>
|
|
|
|
|
|
/// https://stackoverflow.com/a/37755503
|
|
|
|
|
|
/// </seealso>
|
|
|
|
|
|
class WindowsVersion
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the Windows major version number for this computer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static uint WinMajorVersion
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2018-02-11 15:43:58 +01:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
// The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
|
|
|
|
|
|
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out dynamic? major))
|
|
|
|
|
|
return (uint)major;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
|
|
|
|
|
|
// When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
|
2022-07-03 22:34:07 +08:00
|
|
|
|
return 0;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
var versionParts = ((string)version!).Split('.');
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (versionParts.Length != 2) return 0;
|
2022-10-04 11:53:07 +08:00
|
|
|
|
return uint.TryParse(versionParts[0], NumberStyles.Any, CultureInfo.CurrentCulture, out uint majorAsUInt) ? majorAsUInt : 0;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
}
|
2018-02-11 15:43:58 +01:00
|
|
|
|
|
2022-07-03 22:34:07 +08:00
|
|
|
|
return 0;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns the Windows minor version number for this computer.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static uint WinMinorVersion
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2018-02-11 15:43:58 +01:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
// The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10,
|
|
|
|
|
|
// and will most likely (hopefully) be there for some time before MS decides to change this - again...
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber", out dynamic? minor))
|
|
|
|
|
|
return (uint)minor;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
|
2022-07-03 22:34:07 +08:00
|
|
|
|
// When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (!TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out dynamic? version))
|
2022-07-03 22:34:07 +08:00
|
|
|
|
return 0;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
|
2022-10-02 18:31:08 +08:00
|
|
|
|
var versionParts = ((string)version!).Split('.');
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (versionParts.Length != 2) return 0;
|
2022-10-04 11:53:07 +08:00
|
|
|
|
return uint.TryParse(versionParts[1], NumberStyles.Any, CultureInfo.CurrentCulture, out uint minorAsUInt) ? minorAsUInt : 0;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Try retrieving a registry key
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="path">key path</param>
|
|
|
|
|
|
/// <param name="key">Key</param>
|
|
|
|
|
|
/// <param name="value">Value (output)</param>
|
|
|
|
|
|
/// <returns>TRUE if successfully retrieved</returns>
|
2022-10-02 18:31:08 +08:00
|
|
|
|
private static bool TryGetRegistryKey(string path, string key, out dynamic? value)
|
2018-02-11 15:43:58 +01:00
|
|
|
|
{
|
2022-10-02 18:31:08 +08:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2018-02-11 15:43:58 +01:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
value = null;
|
2022-10-02 18:31:08 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
var rk = Registry.LocalMachine.OpenSubKey(path);
|
|
|
|
|
|
if (rk == null) return false;
|
|
|
|
|
|
value = rk.GetValue(key);
|
|
|
|
|
|
return value != null;
|
|
|
|
|
|
}
|
2022-10-02 18:31:08 +08:00
|
|
|
|
catch
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-02-11 15:43:58 +01:00
|
|
|
|
}
|
2022-07-03 22:34:07 +08:00
|
|
|
|
|
|
|
|
|
|
value = null;
|
|
|
|
|
|
return false;
|
2018-02-11 15:43:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|