我需要在我的 C# Windows 应用程序中检测 Windows 8 操作系统并执行一些设置。我知道可以使用 Environment.OSVersion
检测 Windows 7,但是如何检测 Windows 8 呢?
提前感谢。
我需要在我的 C# Windows 应用程序中检测 Windows 8 操作系统并执行一些设置。我知道可以使用 Environment.OSVersion
检测 Windows 7,但是如何检测 Windows 8 呢?
提前感谢。
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8version)
{
// its win8 or higher.
}
好的,各位,似乎微软已经将这段代码标记为已弃用。我在这里留下一个链接,让你们能够了解更多信息。
简而言之,它说:
对于Windows 8及更高版本,始终会有相同版本号的(6,2,9200,0)。而不是寻找Windows版本,请寻找您正在尝试解决的实际功能的存在。
Windows 8或更新版本:
bool IsWindows8OrNewer()
{
var os = Environment.OSVersion;
return os.Platform == PlatformID.Win32NT &&
(os.Version.Major > 6 || (os.Version.Major == 6 && os.Version.Minor >= 2));
}
Version
结构体的>、<、==、<=
等运算符呢? - AgentFire请查看以下问题的答案:如何获取“友好”的操作系统版本名称?
引用的答案:
您可以使用WMI获得产品名称(“Microsoft® Windows Server® 2008 Enterprise”):
using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First();
return name != null ? name.ToString() : "Unknown";
首先,声明一个结构体如下:
[StructLayout(LayoutKind.Sequential)]
public struct OsVersionInfoEx
{
public int dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public UInt16 wServicePackMajor;
public UInt16 wServicePackMinor;
public UInt16 wSuiteMask;
public byte wProductType;
public byte wReserved;
}
您需要使用以下代码:
using System.Runtime.InteropServices;
[DllImport("kernel32", EntryPoint = "GetVersionEx")]
static extern bool GetVersionEx(ref OsVersionInfoEx osVersionInfoEx);
const int VER_NT_WORKSTATION = 1;
var osInfoEx = new OsVersionInfoEx();
osInfoEx.dwOSVersionInfoSize = Marshal.SizeOf(osInfoEx);
try
{
if (!GetVersionEx(ref osInfoEx))
{
throw(new Exception("Could not determine OS Version"));
}
if (osInfoEx.dwMajorVersion == 6 && osInfoEx.dwMinorVersion == 2
&& osInfoEx.wProductType == VER_NT_WORKSTATION)
MessageBox.Show("You've Got windows 8");
}
catch (Exception)
{
throw;
}
不确定这是否正确,因为我只能检查我所拥有的 Windows 8 版本。
int major = Environment.OSVersion.Version.Major;
int minor = Environment.OSVersion.Version.Minor;
if ((major >= 6) && (minor >= 2))
{
//do work here
}
Version.CompareTo
?if( Environment.OSVersion.Version.CompareTo( new Version(6, 2) ) > 0 ) { /* win8 或更高版本 */ }
- Dai