Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Article

OS Name, Version & Product Type

Rate me:
Please Sign up or sign in to vote.
3.48/5 (20 votes)
24 Jul 2005 144.9K   1.8K   30   24
Easy way for retrieving the OS name, version and product type.

Sample Screenshot

Introduction

In my first tutorial I will present an easy way for retrieving the OS name, version and product type. Unfortunately, the .NET Framework doesn't have a way of identifying the OS service pack information and product type. That's why I used in my code the imported Windows API function GetVersionEx along with the System.Environmet.OSVersion property. The entire code consists of three methods, five properties and the additional structures, constants and DLL import. I will try as soon as possible to provide more comments to the code.

The Code

Required Namespaces:

C#
using</FONT> System;
using System.Runtime.InteropServices;

The Structure:

C#
[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
    public int dwOSVersionInfoSize;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string szCSDVersion;
    public short wServicePackMajor;
    public short wServicePackMinor;
    public short wSuiteMask;
    public byte wProductType;
    public byte wReserved;
}

The DLL Import:

C#
[DllImport("kernel32.dll")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

The Constants:

C#
private const int VER_NT_WORKSTATION = 1;
private const int VER_NT_DOMAIN_CONTROLLER = 2;
private const int VER_NT_SERVER = 3;
private const int VER_SUITE_SMALLBUSINESS = 1;
private const int VER_SUITE_ENTERPRISE = 2;
private const int VER_SUITE_TERMINAL = 16;
private const int VER_SUITE_DATACENTER = 128;
private const int VER_SUITE_SINGLEUSERTS = 256;
private const int VER_SUITE_PERSONAL = 512;
private const int VER_SUITE_BLADE = 1024;

The Methods:

C#
public static string GetOSProductType()
{
    OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
    OperatingSystem osInfo = Environment.OSVersion;

    osVersionInfo.dwOSVersionInfoSize = 
           Marshal.SizeOf(typeof(OSVERSIONINFOEX));

    if(!GetVersionEx(ref osVersionInfo))
    {
        return "";
    }
    else
    {
        if(osInfo.Version.Major == 4)
        {
            if(osVersionInfo.wProductType == VER_NT_WORKSTATION)
            {
                // Windows NT 4.0 Workstation
                return " Workstation";
            }
            else if(osVersionInfo.wProductType == VER_NT_SERVER)
            {
                // Windows NT 4.0 Server
                return " Server";
            }
            else
            {
                return "";
            }
        }
        else if(osInfo.Version.Major == 5)
        {
            if(osVersionInfo.wProductType == VER_NT_WORKSTATION)
            {
                if((osVersionInfo.wSuiteMask & 
                    VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
                {
                    // Windows XP Home Edition
                    return " Home Edition";
                }
                else
                {
                    // Windows XP / Windows 2000 Professional
                    return " Professional";
                }
            }
            else if(osVersionInfo.wProductType == VER_NT_SERVER)
            {
                if(osInfo.Version.Minor == 0) 
                { 
                    if((osVersionInfo.wSuiteMask & 
                        VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
                    {
                        // Windows 2000 Datacenter Server
                        return " Datacenter Server";
                    }
                    else if((osVersionInfo.wSuiteMask & 
                             VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
                    {
                        // Windows 2000 Advanced Server
                        return " Advanced Server";
                    }
                    else
                    {
                        // Windows 2000 Server
                        return " Server";
                    }
                } 
            }
            else
            {
                if((osVersionInfo.wSuiteMask & 
                    VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
                {
                    // Windows Server 2003 Datacenter Edition
                    return " Datacenter Edition";
                }
                else if((osVersionInfo.wSuiteMask & 
                         VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
                {
                    // Windows Server 2003 Enterprise Edition
                    return " Enterprise Edition";
                }
                else if((osVersionInfo.wSuiteMask & 
                         VER_SUITE_BLADE) == VER_SUITE_BLADE)
                {
                    // Windows Server 2003 Web Edition
                    return " Web Edition";
                }
                else
                {
                    // Windows Server 2003 Standard Edition
                    return " Standard Edition";
                }
            }
        }
    }

    return "";
}
C#
public static string GetOSServicePack()
{
    OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
    osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
    
    if(!GetVersionEx(ref osVersionInfo))
    {
        return "";
    }
    else
    {
        return " " + osVersionInfo.szCSDVersion;
    }
}
C#
public static string GetOSName()
{
    OperatingSystem osInfo = Environment.OSVersion;
    string osName = "UNKNOWN";


    switch(osInfo.Platform)
    {
        case PlatformID.Win32Windows:
        {
            switch(osInfo.Version.Minor)
            {
                case 0:
                {
                    osName = "Windows 95";
                    break;
                }

                case 10:
                {
                    if(osInfo.Version.Revision.ToString() == "2222A")
                    {
                        osName = "Windows 98 Second Edition";
                    }
                    else
                    {
                        osName = "Windows 98";
                    }
                    break;
                }

                case 90:
                {
                    osName = "Windows Me";
                    break;
                }
            }
            break;
        }

        case PlatformID.Win32NT:
        {
            switch(osInfo.Version.Major)
            {
                case 3:
                {
                    osName = "Windows NT 3.51";
                    break;
                }

                case 4:
                {
                    osName = "Windows NT 4.0";
                    break;
                }

                case 5:
                {
                    if(osInfo.Version.Minor == 0)
                    {
                        osName = "Windows 2000";
                    }
                    else if(osInfo.Version.Minor == 1)
                    {
                        osName = "Windows XP";
                    }
                    else if(osInfo.Version.Minor == 2)
                    {
                        osName = "Windows Server 2003";
                    }
                    break;
                }

                case 6:
                {
                    osName = "Windows Vista";
                    break;
                }
            }
            break;
        }
    }

    return osName;
}

The Properties:

C#
public static string OSVersion
{
    get
    {
        return Environment.OSVersion.Version.ToString();
    }
}
C#
public static int OSMajorVersion
{
    get
    {
        return Environment.OSVersion.Version.Major;
    }
}
C#
public static int OSMinorVersion
{
    get
    {
        return Environment.OSVersion.Version.Minor;
    }
}
C#
public static int OSBuildVersion
{
    get
    {
        return Environment.OSVersion.Version.Build;
    }
}
C#
public static int OSRevisionVersion
{
    get
    {
        return Environment.OSVersion.Version.Revision;
    }
}

Notes

  • OSProductType, GetOSServicePack: The space added to the returning result is for text formatting.
  • OSMajorVersion, OSMinorVersion, OSBuildVersion and OSRevisionVersion: Can be used for comparing OS versions.

I hope this code is useful. I would appreciate comments or bug reports for this code. Thank you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwin8/ win8.1 differents Pin
meirs8419-Mar-14 0:29
meirs8419-Mar-14 0:29 
AnswerRe: win8/ win8.1 differents Pin
Nicke Manarin6-May-14 5:46
Nicke Manarin6-May-14 5:46 
QuestionHow support WinCE Pin
Cloud Hsu6-Dec-11 17:21
Cloud Hsu6-Dec-11 17:21 
GeneralThank you! Pin
Teufel121231-Aug-10 10:58
Teufel121231-Aug-10 10:58 
QuestionHow about Windows 2008 support Pin
Cory R Stein10-May-10 7:26
Cory R Stein10-May-10 7:26 
AnswerRe: How about Windows 2008 support Pin
Vincent DUVERNET (Nolmë Informatique)11-Sep-11 5:20
Vincent DUVERNET (Nolmë Informatique)11-Sep-11 5:20 
Questionhow about windows 7 types? Pin
Nothunt30-Apr-10 6:28
Nothunt30-Apr-10 6:28 
QuestionHow To Add Windows 7 Support! [modified] Pin
MadMan-2972914-Sep-09 14:41
MadMan-2972914-Sep-09 14:41 
GeneralMy vote of 1 Pin
Priyank Bolia11-Apr-09 23:01
Priyank Bolia11-Apr-09 23:01 
General32 bit or 64 bit version Pin
miltash18-May-07 21:17
miltash18-May-07 21:17 
GeneralRe: 32 bit or 64 bit version Pin
Sabin Finateanu1-Jun-07 3:54
Sabin Finateanu1-Jun-07 3:54 
GeneralRe: 32 bit or 64 bit version Pin
birk12327-Nov-07 9:52
birk12327-Nov-07 9:52 
GeneralRe: 32 bit or 64 bit version Pin
neredera31-Mar-08 20:38
neredera31-Mar-08 20:38 
GeneralRe: 32 bit or 64 bit version Pin
Vincent DUVERNET (Nolmë Informatique)11-Sep-11 21:36
Vincent DUVERNET (Nolmë Informatique)11-Sep-11 21:36 
This code works (C++) :

BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;

//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
//handle error
}
}
return bIsWow64;
}
GeneralDomain controller in GetOSProductType() Pin
AE-NQN3-Mar-07 9:02
AE-NQN3-Mar-07 9:02 
GeneralRe: Domain controller in GetOSProductType() Pin
Sabin Finateanu1-Jun-07 3:54
Sabin Finateanu1-Jun-07 3:54 
GeneralWindows 98 SE Pin
Moski15-Dec-05 2:13
Moski15-Dec-05 2:13 
GeneralRe: Windows 98 SE Pin
scienty774-Jun-09 21:13
scienty774-Jun-09 21:13 
GeneralBug identifying Server 2003 Pin
Member 1119107313-Oct-05 12:26
Member 1119107313-Oct-05 12:26 
AnswerRe: Bug identifying Server 2003 Pin
Sabin Finateanu14-Oct-05 2:18
Sabin Finateanu14-Oct-05 2:18 
GeneralVista Pin
Marc Clifton25-Jul-05 2:07
mvaMarc Clifton25-Jul-05 2:07 
GeneralRe: Vista Pin
Sabin Finateanu25-Jul-05 2:31
Sabin Finateanu25-Jul-05 2:31 
GeneralRe: Vista Pin
great_scandinavian25-Feb-07 9:50
great_scandinavian25-Feb-07 9:50 
GeneralRe: Vista Pin
LordWilson17-Dec-08 12:29
LordWilson17-Dec-08 12:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.