简体   繁体   English

使用C#获取操作系统

[英]Getting Operating System using c#

I've been conducting research on a way of getting the current system operating system using C# . 我一直在研究使用C#获取当前系统操作系统的方法。

It seems there isn't one single answer, as in many cases, but they all seem to be a few years old. 在许多情况下,似乎没有一个单一的答案,但它们似乎都存在了几年。 Some which I attempted to use and base my code around, didn't work . 我尝试使用某些代码并以此为基础的某些代码不起作用

References: 1 , 2 , 3 , 4 , 5 , etc 参考文献: 12345

So I came up with my own code: 所以我想出了自己的代码:

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

I have tested this code on my laptop, running windows 7, it returns windows 2008 . 我已经在运行Windows 7的笔记本电脑上测试了此代码,它将返回windows 2008 What could be the cause of this, is there anything I should change? 这可能是什么原因,我应该改变什么吗?

Because 6.1 is for Windows 7 and Windows Server 2008, 6.2 is Windows 8. 由于6.1适用于Windows 7和Windows Server 2008,因此6.2适用于Windows 8。

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

According to Microsoft https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx 根据Microsoft https://msdn.microsoft.com/zh-CN/library/windows/desktop/ms724832(v=vs.85).aspx

In your situation, 6.1 is refer to Window 7/Windows Server 2008 R2 根据您的情况,6.1是指Window 7/Windows Server 2008 R2

Looking at this article , Windows 7 is actually 6.1, so I guess your case statement is not valid. 查看本文 ,Windows 7实际上是6.1,因此我想您的case语句无效。 There's no way, using the version, to differenciate Windows 7 from windows 2008 server 无法使用该版本来区分Windows 7与Windows 2008 Server

Windows官方列表中可以看到,版本6.1是Windows 7但是您的代码说它是Windows 2008 (顺便说一下,这不是问题。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM