在底层,
Environment.OSVersion 使用了已经被弃用的
GetVersionEx 函数。文档警告了你所观察到的行为:
未经过 Windows 8.1 或 Windows 10 认证的应用将返回 Windows 8 操作系统版本号 (6.2)。
文档还建议:
识别当前操作系统通常不是确定特定操作系统功能是否存在的最佳方式。这是因为操作系统可能在可重新分配的 DLL 中添加了新功能。与其使用 GetVersionEx 确定操作系统平台或版本号,不如测试功能本身的存在性。
如果上述建议不适用于你的情况,并且你确实想要检查实际运行的操作系统版本,则文档也提供了有关此事的提示:
为了比较当前系统版本和所需版本,请使用 VerifyVersionInfo 函数而不是使用 GetVersionEx 自行执行比较。
以下文章发布了使用
VerifyVersionInfo 函数的有效解决方案:
.NET 的版本助手 API。
完全归功于该文章的作者,以下代码段应提供你所寻找的行为:
public class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsWindowsVersionOrGreater(6, 3, 0));
}
[StructLayout(LayoutKind.Sequential)]
struct OsVersionInfoEx
{
public uint OSVersionInfoSize;
public uint MajorVersion;
public uint MinorVersion;
public uint BuildNumber;
public uint PlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string CSDVersion;
public ushort ServicePackMajor;
public ushort ServicePackMinor;
public ushort SuiteMask;
public byte ProductType;
public byte Reserved;
}
[DllImport("kernel32.dll")]
static extern ulong VerSetConditionMask(ulong dwlConditionMask,
uint dwTypeBitMask, byte dwConditionMask);
[DllImport("kernel32.dll")]
static extern bool VerifyVersionInfo(
[In] ref OsVersionInfoEx lpVersionInfo,
uint dwTypeMask, ulong dwlConditionMask);
static bool IsWindowsVersionOrGreater(
uint majorVersion, uint minorVersion, ushort servicePackMajor)
{
OsVersionInfoEx osvi = new OsVersionInfoEx();
osvi.OSVersionInfoSize = (uint)Marshal.SizeOf(osvi);
osvi.MajorVersion = majorVersion;
osvi.MinorVersion = minorVersion;
osvi.ServicePackMajor = servicePackMajor;
const uint VER_MINORVERSION = 0x0000001;
const uint VER_MAJORVERSION = 0x0000002;
const uint VER_SERVICEPACKMAJOR = 0x0000020;
const byte VER_GREATER_EQUAL = 3;
ulong versionOrGreaterMask = VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(
0, VER_MAJORVERSION, VER_GREATER_EQUAL),
VER_MINORVERSION, VER_GREATER_EQUAL),
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
uint versionOrGreaterTypeMask = VER_MAJORVERSION |
VER_MINORVERSION | VER_SERVICEPACKMAJOR;
return VerifyVersionInfo(ref osvi, versionOrGreaterTypeMask,
versionOrGreaterMask);
}
}
免责声明:我尚未拥有 Windows 10,因此尚未在 Windows 10 上测试过此代码。
FEATURE_BROWSER_EMULATION
设置为12000
或12001
吗? - noseratio - open to work