0

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?

Community
  • 1
  • 1
099
  • 333
  • 1
  • 15
  • The code is simply wrong, it's saying that version 6.1 is `Windows 2008` instead of `Windows 7`. It also doesn't cope with `Windows 8` in any way.# – DavidG Jul 01 '15 at 08:44
  • Well *Windows 7* is **NT 6.1** as well as *Windows Server 2008 R2* is. Check out this list for further information: [List of Microsoft Windows versions](https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions) – MrPaulch Jul 01 '15 at 08:45

4 Answers4

3

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

Manuel
  • 78
  • 6
2

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

Liem Do
  • 933
  • 1
  • 7
  • 13
1

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

Dimitri Mockelyn
  • 1,535
  • 2
  • 11
  • 17
1

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

DavidG
  • 113,891
  • 12
  • 217
  • 223