099
099

Reputation: 333

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?

Upvotes: 0

Views: 619

Answers (4)

Liem Do
Liem Do

Reputation: 943

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

Upvotes: 2

DavidG
DavidG

Reputation: 119136

From the official list of Windows versions, version 6.1 is Windows 7 but your code says it is Windows 2008 (which by the way isn't a thing.)

Upvotes: 1

Manuel
Manuel

Reputation: 78

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

Upvotes: 3

Dimitri Mockelyn
Dimitri Mockelyn

Reputation: 1545

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

Upvotes: 1

Related Questions