如何使用 C# 4.0 检测 Windows 8 操作系统?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13620223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect Windows 8 Operating system using C# 4.0?
提问by Rajesh Subramanian
I have to detect Windows 8 Operating system in my C# Windows Application and do some settings. I know we can detect Windows 7 using Environment.OSVersion, but how can windows 8 be detected?
我必须在我的 C# Windows 应用程序中检测 Windows 8 操作系统并进行一些设置。我知道我们可以使用 检测 Windows 7 Environment.OSVersion,但是如何检测 Windows 8?
Thanks in advance.
提前致谢。
采纳答案by AgentFire
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8version)
{
// its win8 or higher.
}
Update for windows 8.1 and higher:
Windows 8.1 及更高版本的更新:
Okay guys, it seems to me that this piece of code has been marked as deprecated by Microsoft itself. I leave a link hereso you could read more about it.
好的,在我看来,这段代码已被 Microsoft 标记为已弃用。我在这里留下一个链接,以便您可以阅读更多相关信息。
In short, it says:
简而言之,它说:
For windows 8 and higher, there always will be the same version number of (6, 2, 9200, 0). And rather than looking for Windows version, go look for the actual feature which existance you are trying to resolve.
对于 Windows 8 及更高版本,(6, 2, 9200, 0) 的版本号始终相同。与其查找 Windows 版本,不如查找您要解决的实际功能。
回答by Christian Westman
Check the answer to the following question: How to get the "friendly" OS Version Name?
检查以下问题的答案:如何获得“友好的”操作系统版本名称?
Quoted answer:
引用的答案:
You can use WMI to get the product name ("Microsoft? Windows Server? 2008 Enterprise "):
您可以使用 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";
回答by Bruno
Start by declaring a struct as follows:
首先声明一个结构体,如下所示:
[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;
}
You will need this using statement:
您将需要此 using 语句:
using System.Runtime.InteropServices;
At the top of your relevant class, declare:
在相关类的顶部,声明:
[DllImport("kernel32", EntryPoint = "GetVersionEx")]
static extern bool GetVersionEx(ref OsVersionInfoEx osVersionInfoEx);
Now call the code as follows:
现在调用代码如下:
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;
}
回答by Tsukasa
Not sure if this is correct as I can only check on the version of windows 8 I have.
不确定这是否正确,因为我只能检查我拥有的 Windows 8 版本。
int major = Environment.OSVersion.Version.Major;
int minor = Environment.OSVersion.Version.Minor;
if ((major >= 6) && (minor >= 2))
{
//do work here
}
回答by Asik
Windows 8 or more recent:
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));
}

