18

What is the best to determine the Microsoft OS that is hosting your ASP.NET application using the System.Environment.OSVersion namespace

I need an example for Windows XP, Windows Server 2003 and Windows Vista

Here is what I am trying to accomplish using pseudocode

switch(/* Condition for determining OS */)
{
    case "WindowsXP":
        //Do Windows XP stuff
        break;
    case "Windows Server 2003":
        //Do Windows Server 2003 stuff
        break;
    case "Windows Vista":
        //Do Windows Vista stuff
        break;
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

4 Answers4

18

The following should work. But why do you care? Is just for informational purposes in logging or are you looking for actual capabilities being present on the target platform?

if (Environment.OSVersion.Version.Major == 5)
{
    if (Environment.OSVersion.Version.Minor == 1)
    {
             // XP
    }
    else if (Environment.OSVersion.Version.Minor == 2)
    {
             // Server 2003.  XP 64-bit will also fall in here.
    }
}
else if (Environment.OSVersion.Version.Major >= 6)
{
        // Vista on up
}
DiscDev
  • 38,652
  • 20
  • 117
  • 133
Michael
  • 54,279
  • 5
  • 125
  • 144
10

Not a complete list, but got this from http://support.microsoft.com/kb/304283:

+--------------------------------------------------------------+
|           |Windows|Windows|Windows|Windows NT|Windows|Windows|
|           |  95   |  98   |  Me   |    4.0   | 2000  |  XP   |
+--------------------------------------------------------------+
|PlatformID | 1     | 1     | 1     | 2        | 2     | 2     |
+--------------------------------------------------------------+
|Major      |       |       |       |          |       |       |
| version   | 4     | 4     | 4     | 4        | 5     | 5     |
+--------------------------------------------------------------+
|Minor      |       |       |       |          |       |       |
| version   | 0     | 10    | 90    | 0        | 0     | 1     |
+--------------------------------------------------------------+

Edit: Note, the information returned by System.Environment.OSVersion may be unreliable if the application is running in compatibility mode.

Edit2: I would recommend you just make it a configurable value in your application - that way your code does not need recompilation when a new OS comes out, e.g., Windows 7.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
3

If you dont have to be specific about R2 of server editions, a simpler alternative is:

enum OS { _2000, XP, Server2003, Vista, Server2008, _7, Server2012, _8 }

const int OS_ANYSERVER = 29;

[DllImport("shlwapi.dll", SetLastError = true, EntryPoint = "#437")]
static extern bool IsOS(int os);

static bool isWindowsServer = IsOS(OS_ANYSERVER);

public static OS GetOS()
{
    var version = Environment.OSVersion.Version;
    switch (version.Major)
    {
        case 5:
            switch (version.Minor)
            {
                case 0:
                    return OS._2000;
                case 1:
                    return OS.XP;
                case 2:
                    return isWindowsServer ? OS.Server2003 : OS.XP;
            }
            break;
        case 6:
            switch (version.Minor)
            {
                case 0:
                    return isWindowsServer ? OS.Server2008 : OS.Vista;
                case 1:
                    return isWindowsServer ? OS.Server2008 : OS._7;
                case 2:
                    return isWindowsServer ? OS.Server2012 : OS._8;
            }
            break;
    }

    throw new Exception("Strange OS");
}

Copied from here.

To be more specific your options are,

  1. WMI, you will have to some manual parsing. Not sure if user privilege is going to hurt non admin users.

  2. GetVersionEx as described in this answer.

  3. Checking for ProductName at

    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\
    
Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
2
if(Environment.OSVersion.Version.Major > 5) { /* vista and above */ }
Garrett
  • 1,750
  • 2
  • 16
  • 23