簡體   English   中英

在 C# 中識別操作系統詳細信息

[英]Identify the Operating System details in C#

如何在 WPF 應用程序中使用 C# 代碼獲取操作系統詳細信息?

Environment類提供可用於獲取系統信息的屬性

您可以從系統獲取操作系統信息。 Environment.OSVersion 這里

看看System.Environment它有屬性OSVersion

由於我只需要關心非服務器版本,我這樣做:

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本答案所述

  3. 檢查ProductName

    HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\
  4. IsOS函數,如本答案所述 我最喜歡這個..

在這里提供了更完整的答案。

使用注冊表對於任何應用程序都是可能的。 在 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;
            }
        }
    }
}

System.Runtime.InteropServices.RuntimeInformation.OSDescription

亞歷克斯·桑索的回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM