【问题标题】:How to detect Windows 8 Operating system using C# 4.0?如何使用 C# 4.0 检测 Windows 8 操作系统?
【发布时间】:2012-11-17 04:45:45
【问题描述】:

我必须在我的 C# Windows 应用程序中检测 Windows 8 操作系统并进行一些设置。我知道我们可以使用Environment.OSVersion 检测到 Windows 7,但是如何检测到 Windows 8?

提前致谢。

【问题讨论】:

  • 您是否尝试过使用 Environment.OSVersion 并检查它在 Windows 8 上产生的结果?
  • 它打印 Microsoft Windows NT 6.2.9200.0
  • 那你不能用吗?
  • 谢谢。好主意。我会用那个
  • 您是否考虑过哪个答案适合您?如果您接受了这个答案,那就太好了。

标签: c# .net windows winforms


【解决方案1】:
Version win8version = new Version(6, 2, 9200, 0);

if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
    Environment.OSVersion.Version >= win8version)
{
    // its win8 or higher.
}

Windows 8.1 及更高版本的更新:

好的,伙计们,在我看来,这段代码已被 Microsoft 自己标记为已弃用。我留下了link here,以便您阅读更多相关信息。

简而言之,它说:

对于 Windows 8 及更高版本,(6, 2, 9200, 0) 的版本号始终相同。与其寻找 Windows 版本,不如寻找您要解决的实际功能。

【讨论】:

  • 在我的 Windows 应用商店应用中未找到 OSVersion、未找到 Environment.OSVersion、未找到 PlatformID。因此,对于 Windows 应用商店应用程序,这将不起作用。不好。
  • @Tertium 问题中没有 Windows Store 标签。
  • OSVersion.Platform 应该是 Environment.OSVersion.Platform
  • 在 Windows 10 上请注意这一点:它返回与 Windows 8 相同的结果
  • @superjos 是 (6, 2, 9200, 0)?
【解决方案2】:

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));
}

【讨论】:

  • 实际上应该是: return os.Platform == PlatformID.Win32NT && (os.Version.Major > 6 || (os.Version.Major == 6 && os.Version.Minor >= 2 ));
  • 为什么不直接使用Version结构的> or < or == or <= etc操作符?
  • 因为我在写答案时并没有意识到这些,并且在您使用这些提供答案后不久。你的答案更好,但我把我的作为替代选择。
【解决方案3】:

检查以下问题的答案:How to get the "friendly" OS Version Name?

引用答案:

您可以使用 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";

【讨论】:

  • 感谢您的回答。但我不想使用 WMI。
【解决方案4】:

不确定这是否正确,因为我只能检查我拥有的 Windows 8 版本。

 int major = Environment.OSVersion.Version.Major;
 int minor = Environment.OSVersion.Version.Minor;

if ((major >= 6) && (minor >= 2))
{
    //do work here
}

【讨论】:

  • major = 7 和 minor = 0 会发生什么?
  • 最好只获取版本和子字符串(Environment.OSVersion.Version.ToString().Substring(0,3)),然后比较版本表(msdn.microsoft.com/en-us/library/windows/desktop/…)。
  • 您可以根据需要更改运算符,但从我发布的内容来看,它将接受 windows 8 / server 2012 以上的任何内容。正如您从上面发布的 MSDN 链接中看到的那样,它们目前只有 6.2.xx跨度>
  • 为什么不使用Version.CompareToif( Environment.OSVersion.Version.CompareTo( new Version(6, 2) ) &gt; 0 ) { /* win8 or later */ }
  • 似乎需要混合 Asik 和 @AngentFire 的解决方案。尝试重新发明 Version 类已经实现的 >= 运算符是没有意义的。当您尝试过度优化时,总是会令人惊讶地发现这样的错误是多么容易。
【解决方案5】:

首先声明一个结构体,如下所示:

[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 语句:

    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;
        }

【讨论】:

  • 与内置的 .Net API 相比过于复杂。 :)
  • 这实际上是检测 Win8 并区分标准/工作站和服务器版本的唯一方法。
猜你喜欢
  • 2011-05-20
  • 2012-03-22
  • 1970-01-01
  • 2011-05-21
  • 2012-05-18
  • 2012-04-24
  • 1970-01-01
  • 2012-04-01
相关资源
最近更新 更多