在C#中识别操作系统详细信息

时间:2009-09-11 10:04:33

标签: c# wpf operating-system

如何在WPF应用程序中使用C#代码获取操作系统详细信息?

6 个答案:

答案 0 :(得分:15)

Environment类提供可用于获取系统信息的properties

答案 1 :(得分:6)

您可以从系统获取操作系统信息。 Environment.OSVersion Here

答案 2 :(得分:4)

查看System.Environment它有属性OSVersion

答案 3 :(得分:2)

由于我只需关心非服务器版本,我这样做:

enum OS { _2000, XP, Vista, _7, _8 }

public static OS GetOS()
{
    var version = Environment.OSVersion.Version;
    switch (version.Major)
    {
        case 5:
            switch (version.Minor)
            {
                case 0:
                    return OS._2000;
                case 1:
                    return OS.XP;
                case 2:
                    return OS.XP; //could also be Server 2003, Server 2003 R2
            }
            break;
        case 6:
            switch (version.Minor)
            {
                case 0:
                    return OS.Vista; //could also be Server 2008
                case 1:
                    return OS._7; //could also be Server 2008 R2
                case 2:
                    return OS._8; //could also be Server 20012, Server 2012 R2
            }
            break;
    }

    throw new Exception("Strange OS");
}

如果您确实需要考虑服务器版本,那么您的选择是:

  1. WMI,您将需要进行一些手动解析。不确定用户权限是否会伤害非管理员用户。

  2. GetVersionEx,如this answer

  3. 中所述
  4. 处检查ProductName
    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
    
  5. IsOS功能,如this answer中所述。我更喜欢这个..

  6. 我提供了更完整的答案here

答案 4 :(得分:0)

使用注册表是任何应用程序都可以使用的。在C#中,我为自己做了一个实用工具类。请注意,Microsoft已更改了Windows 10+的密钥(此类已设计为已处理)。我认为课程应该为您提供所需的信息:

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

答案 5 :(得分:-1)

System.Runtime.InteropServices.RuntimeInformation.OSDescription

Alex Sanséau's answer