【问题标题】:Getting Operating System using c#使用 c# 获取操作系统
【发布时间】:2015-09-18 07:44:42
【问题描述】:

我一直在研究一种使用C# 获取当前系统操作系统的方法。

在许多情况下,似乎没有一个单一的答案,但它们似乎都有几年的历史了。我尝试使用的一些代码并以此为基础,不起作用

参考:12345

所以我想出了自己的代码:

var os = System.Environment.OSVersion;
string currentOS = null;

switch(os.Platform)
{
    case PlatformID.Win32S:
        currentOS = "Windows 3.1";
        break;
    case PlatformID.Win32Windows:
        switch(os.Version.Minor)
        {
            case 0:
                currentOS = "Windows 95";
                break;
            case 10:
                currentOS = "Windows 98";
                break;
            default:
                currentOS = "Unknown";
                break;
        }
        break;
    case PlatformID.Win32NT:
        switch(os.Version.Major)
        {
            case 3:
                currentOS = "Windows NT 3.51";
                break;
            case 4:
                currentOS = "Windows NT 4.0";
                break;
            case 5:
                switch(os.Version.Minor)
                {
                    case 0:
                        currentOS = "Windows 2000";
                        break;
                    case 1:
                        currentOS = "Windows XP";
                        break;
                    case 2:
                        currentOS = "Windows 2003";
                        break;
                    default:
                        currentOS = "Unknown";
                        break;
                }
                break;
            case 6:
                switch(os.Version.Minor)
                {
                    case 0:
                        currentOS = "Windows Vista";
                        break;
                    case 1:
                        currentOS = "Windows 2008";
                        break;
                    case 2:
                        currentOS = "Windows 7";
                        break;
                    default:
                        currentOS = "Unknown";
                        break;
                }
                break;
            default:
                currentOS = "Unknown";
                break;
        }
        break;
    case PlatformID.WinCE:
        currentOS = "Windows CE";
        break;
    case PlatformID.Unix:
        currentOS = "Unix";
        break;
    default:
        currentOS = "Unknown";
        break;
}

我在运行 Windows 7 的笔记本电脑上测试了此代码,它返回 windows 2008。这可能是什么原因,有什么我应该改变的吗?

【问题讨论】:

  • 代码完全是错误的,它是说6.1版本是Windows 2008而不是Windows 7。它也无法以任何方式处理Windows 8。#
  • 好吧,Windows 7NT 6.1 以及 Windows Server 2008 R2。查看此列表以获取更多信息:List of Microsoft Windows versions

标签: c#


【解决方案1】:

因为 6.1 适用于 Windows 7 和 Windows Server 2008,所以 6.2 适用于 Windows 8。

来源:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx

【讨论】:

    【解决方案2】:

    根据微软 https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx

    在你的情况下,6.1 是指Window 7/Windows Server 2008 R2

    【讨论】:

      【解决方案3】:

      查看this article,Windows 7 实际上是 6.1,所以我猜你的 case 语句无效。使用版本无法区分 Windows 7 和 Windows 2008 服务器

      【讨论】:

        【解决方案4】:

        official list of Windows versions 开始,版本 6.1 是 Windows 7,但您的代码显示它是 Windows 2008(顺便说一句,这不是问题。)

        【讨论】:

          猜你喜欢
          • 2011-08-08
          • 1970-01-01
          • 1970-01-01
          • 2015-05-25
          • 1970-01-01
          • 2021-11-03
          • 1970-01-01
          • 2011-03-05
          • 1970-01-01
          相关资源
          最近更新 更多