C# 使用 Environment.OSVersion 确定操作系统

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/860459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:35:09  来源:igfitidea点击:

Determine OS using Environment.OSVersion

c#asp.netwindows

提问by Michael Kniskern

What is the best to determine the Microsoft OS that is hosting your ASP.NET application using the System.Environment.OSVersionnamespace

使用System.Environment.OSVersion命名空间 确定托管 ASP.NET 应用程序的 Microsoft 操作系统的最佳方法是什么

I need an example for Windows XP, Windows Server 2003 and Windows Vista

我需要一个适用于 Windows XP、Windows Server 2003 和 Windows Vista 的示例

Here is what I am trying to accomplish using pseudocode

这是我尝试使用伪代码完成的工作

switch(/* Condition for determining OS */)
{
    case "WindowsXP":
        //Do Windows XP stuff
        break;
    case "Windows Server 2003":
        //Do Windows Server 2003 stuff
        break;
    case "Windows Vista":
        //Do Windows Vista stuff
        break;
}

采纳答案by Michael

The following should work. But why do you care? Is just for informational purposes in logging or are you looking for actual capabilities being present on the target platform?

以下应该工作。但你为什么要关心?仅用于日志记录的信息目的还是您正在寻找目标平台上存在的实际功能?

if (Environment.OSVersion.Version.Major == 5)
{
    if (Environment.OSVersion.Version.Minor == 1)
    {
             // XP
    }
    else if (Environment.OSVersion.Version.Minor == 2)
    {
             // Server 2003.  XP 64-bit will also fall in here.
    }
}
else if (Environment.OSVersion.Version.Major >= 6)
{
        // Vista on up
}

回答by Garrett

if(Environment.OSVersion.Version.Major > 5) { /* vista and above */ }

回答by RedFilter

Not a complete list, but got this from http://support.microsoft.com/kb/304283:

不是完整列表,而是从http://support.microsoft.com/kb/304283得到的:

+--------------------------------------------------------------+
|           |Windows|Windows|Windows|Windows NT|Windows|Windows|
|           |  95   |  98   |  Me   |    4.0   | 2000  |  XP   |
+--------------------------------------------------------------+
|PlatformID | 1     | 1     | 1     | 2        | 2     | 2     |
+--------------------------------------------------------------+
|Major      |       |       |       |          |       |       |
| version   | 4     | 4     | 4     | 4        | 5     | 5     |
+--------------------------------------------------------------+
|Minor      |       |       |       |          |       |       |
| version   | 0     | 10    | 90    | 0        | 0     | 1     |
+--------------------------------------------------------------+

Edit: Note, the information returned by System.Environment.OSVersion may be unreliable if the application is running in compatibility mode.

编辑:请注意,如果应用程序在兼容模式下运行,则 System.Environment.OSVersion 返回的信息可能不可靠。

Edit2: I would recommend you just make it a configurable value in your application - that way your code does not need recompilation when a new OS comes out, e.g., Windows 7.

Edit2:我建议你在你的应用程序中让它成为一个可配置的值——这样当一个新的操作系统出现时你的代码不需要重新编译,例如,Windows 7。

回答by nawfal

If you dont have to be specific about R2 of server editions, a simpler alternative is:

如果您不必特定于服务器版本的 R2,则更简单的替代方法是:

enum OS { _2000, XP, Server2003, Vista, Server2008, _7, Server2012, _8 }

const int OS_ANYSERVER = 29;

[DllImport("shlwapi.dll", SetLastError = true, EntryPoint = "#437")]
static extern bool IsOS(int os);

static bool isWindowsServer = IsOS(OS_ANYSERVER);

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 isWindowsServer ? OS.Server2003 : OS.XP;
            }
            break;
        case 6:
            switch (version.Minor)
            {
                case 0:
                    return isWindowsServer ? OS.Server2008 : OS.Vista;
                case 1:
                    return isWindowsServer ? OS.Server2008 : OS._7;
                case 2:
                    return isWindowsServer ? OS.Server2012 : OS._8;
            }
            break;
    }

    throw new Exception("Strange OS");
}

Copied from here.

这里复制。

To be more specific your options are,

更具体地说,您的选择是,

  1. WMI, you will have to some manual parsing. Not sure if user privilege is going to hurt non admin users.

  2. GetVersionExas described in this answer.

  3. Checking for ProductNameat

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
    
  1. WMI,您将不得不进行一些手动解析。不确定用户权限是否会伤害非管理员用户。

  2. GetVersionEx本答案所述

  3. 检查ProductName

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\