简体   繁体   中英

Getting Operating System using c#

I've been conducting research on a way of getting the current system operating system using 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

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 . 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.

Source: https://msdn.microsoft.com/en-us/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

In your situation, 6.1 is refer to 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. There's no way, using the version, to differenciate Windows 7 from windows 2008 server

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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