tags:

views:

2198

answers:

3

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;
}
+1  A: 
if(Environment.OSVersion.Version.Major > 5) { /* vista and above */ }
Garrett
I need to do different things for each OS version. I was thinking a switch or if/else statement
Michael Kniskern
+2  A: 

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.

RedFilter
+8  A: 

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.Minor == 1)
    {
             // XP
    }
    else if (Environment.OSVersion.Minor == 2)
    {
             // Server 2003.  XP 64-bit will also fall in here.
    }
}
else if (Environment.OSVersion.Version.Major >= 6)
{
        // Vista on up
}
Michael
I need to generate the correct IE user agent string based on which OS is hosting my windows service
Michael Kniskern
Isn't version major/minor numbers sufficient then?like this:Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)
Michael