c#中的Windows版本

Die*_*ego 9 .net c# operating-system windowsversion

我想知道PC有哪个Windows版本..在C#Framework 3.5中

我试过用

OperatingSystem os = Environment.OSVersion;

版本ver = os.Version;

但结果是

Plataform:WIN32NT

版本6.2.9200

版本未成年人:2

版本专业:6

问题是我有"Windows 8 Pro"......

我该如何检测它?

谢谢

Cro*_*ono 12

您必须自己将版本号与相应的字符串值进行匹配.

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

  • Windows Server 2016技术预览版 - 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 OS版本值(6.2).

这是源头.

另外,来自同一来源:

识别当前操作系统通常不是确定特定操作系统功能是否存在的最佳方法.这是因为操作系统可能在可再发行的DLL中添加了新功能.不是使用Version API Helper函数来确定操作系统平台或版本号,而是测试功能本身的存在.


Spi*_*lis 5

在我的场景中,我需要我的应用程序来捕获可能的错误报告和统计信息的计算机信息.

我没有找到必须添加应用程序清单的解决方案.不幸的是,我在google搜索时发现的大多数建议都表明了这一点.

事实上,当使用清单时,必须手动添加每个操作系统版本,以便该特定操作系统版本能够在运行时报告自身.

换句话说,这成为竞争条件:我的应用程序的用户可能正在使用我的应用程序的版本,该应用程序在使用之前的操作系统之前.当微软推出新的操作系统版本时,我必须立即升级应用程序.我还必须强制用户在更新操作系统的同时升级应用程序.

换句话说,不太可行.

在浏览选项后,我发现了一些引用(与应用清单相比很少见)而是建议使用注册表查找.

我(砍掉)ComputerInfo类只WinMajorVersion,WinMinorVersionIsServer性质是这样的:

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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)