C#中的Windows版本

10

我想知道电脑运行的Windows版本,使用的是C# Framework 3.5。

我试过下面这段代码:

OperatingSystem os = Environment.OSVersion;

Version ver = os.Version;

但结果却是:

平台:WIN32NT

版本:6.2.9200

版本次要号:2

版本主要号:6

问题在于我用的是“Windows 8 Pro”...

如何检测它呢?

谢谢


4
操作系统版本:http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724832(v=vs.85).aspx - DavidN
1
没问题。有人只是不知道Windows内部编号与市场名称不同而已。 - TomTom
4个回答

13

您需要自行匹配版本号与相应的字符串值。

以下是最新的Windows操作系统及其对应的版本号列表:

  • Windows Server 2016和2019 - 10.0*
  • Windows 10 - 10.0*
  • Windows 8.1 - 6.3*
  • Windows Server 2012 R2 - 6.3*
  • Windows 8 - 6.2
  • Windows Server 2012 - 6.2
  • Windows 7 - 6.1
  • Windows Server 2008 R2 - 6.1
  • Windows Server 2008 - 6.0
  • Windows Vista - 6.0
  • Windows Server 2003 R2 - 5.2
  • Windows Server 2003 - 5.2
  • Windows XP 64位版 - 5.2
  • Windows XP - 5.1
  • Windows 2000 - 5.0

*对于已为Windows 8.1或10显式清单化的应用程序。未显式清单化8.1/10的应用程序将返回Windows 8操作系统版本值(6.2)。

这里是出处

同样来自于相同的出处:

通常,识别当前操作系统并不是确定特定操作系统功能是否存在的最佳方法。这是因为操作系统可以在可重分发的DLL中添加新功能。与其使用版本API Helper函数来确定操作系统平台或版本,不如使用数字,测试特性本身的存在。


对于测试功能本身的警告点赞。这让我想到了一种不同的解决问题的方式,可以避免处理版本号带来的痛苦(我只需捕获当功能不存在时抛出的错误,并从那里处理事情)。 - Paul

5
在我的场景中,我需要我的应用程序捕获计算机信息以进行可能的错误报告和统计。然而,我并没有找到添加应用程序清单的解决方案令人满意。大部分在谷歌上找到的建议都是这样的。问题在于,使用清单时,每个操作系统版本都必须手动添加到其中,以便该特定操作系统版本能够在运行时报告自己。换句话说,这变成了一种竞争条件:我的应用程序用户很可能正在使用比操作系统更早的版本。当微软推出新的操作系统版本时,我必须立即升级应用程序。我还必须强制用户在更新操作系统的同时升级应用程序。因此,这不是非常可行的。浏览了选项后,与应用程序清单相比,我发现一些参考资料(惊人地少)建议使用注册表查找。我的(简化的)ComputerInfo类,只包含WinMajorVersion、WinMinorVersion和IsServer属性,看起来像这样:
using Microsoft.Win32;

namespace Inspection
{
    /// <summary>
    /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup.
    /// </summary>
    public static class ComputerInfo
    {
        /// <summary>
        ///     Returns the Windows major version number for this computer.
        /// </summary>
        public static uint WinMajorVersion
        {
            get
            {
                dynamic major;
                // 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...
                if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major))
                {
                    return (uint) major;
                }

                // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint majorAsUInt;
                return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns the Windows minor version number for this computer.
        /// </summary>
        public static uint WinMinorVersion
        {
            get
            {
                dynamic minor;
                // 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...
                if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber",
                    out minor))
                {
                    return (uint) minor;
                }

                // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion'
                dynamic version;
                if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version))
                    return 0;

                var versionParts = ((string) version).Split('.');
                if (versionParts.Length != 2) return 0;
                uint minorAsUInt;
                return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0;
            }
        }

        /// <summary>
        ///     Returns whether or not the current computer is a server or not.
        /// </summary>
        public static uint IsServer
        {
            get
            {
                dynamic installationType;
                if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType",
                    out installationType))
                {
                    return (uint) (installationType.Equals("Client") ? 0 : 1);
                }

                return 0;
            }
        }

        private static bool TryGeRegistryKey(string path, string key, out dynamic value)
        {
            value = null;
            try
            {
                var rk = Registry.LocalMachine.OpenSubKey(path);
                if (rk == null) return false;
                value = rk.GetValue(key);
                return value != null;
            }
            catch
            {
                return false;
            }
        }
    }
}

3

我发布了OsInfo nuget,方便比较Windows版本。

bool win8OrLess = Environment.OSVersion.IsLessThanOrEqualTo(OsVersion.Win8);
bool winXp = Environment.OSVersion.IsEqualTo(OsVersion.WinXP);
int? servicePack = Environment.OSVersion.GetServicePackVersion();
bool is64bit = Environment.OSVersion.Is64Bit(); // Already covered in .NET 4.5+

你有计划增加对Windows 10的支持吗? - mecsco
请在 GitHub 上创建一个问题并在那里进行讨论。我相信这很容易添加。如果您发送拉取请求,我至少会将其合并并发布一个新的 NuGet。 - angularsen
感谢回复 - 提出了一个问题 - https://github.com/anjdreas/OsInfo/issues/1 - mecsco

2

试试这个:

using System.Management;

private string fnGetFriendlyName()
{
    var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
                select x.GetPropertyValue("Caption")).FirstOrDefault();
    return name != null ? name.ToString() : "Unknown";
}

来源: https://dev59.com/CnRB5IYBdhLWcg3wl4Oc#2016557

在IT技术中,“GIL”是全局解释器锁的缩写。它是CPython解释器的一部分,控制同一时刻只有一个线程可以执行Python字节码。这意味着即使在多核CPU上运行Python程序,也不会实现真正的并行处理。因此,在需要高性能的情况下,应该使用多进程或其他并发模型来代替多线程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接