6

How to distinguish the server version from the client version of Windows? Example: XP, Vista, 7 vs Win2003, Win2008.

UPD: Need a method such as

bool IsServerVersion()
{
    return ...;
}
  • 1
    What's the reason for needing to know if the machine is a server or client? In what way will you need to behave differently? – Mike Goatly Jun 16 '11 at 07:28
  • 1
    @Mike: One common usage scenario is licensing: $99 Workstation Edition, $999 Server Edition, no functional differences. Sad, but true. – Heinzi Jun 16 '11 at 07:59
  • @Heinzi - interesting point, I hadn't considered that. I guess that just goes to show why I'm not in marketing! – Mike Goatly Jun 16 '11 at 08:08
  • See also for antivirus is $0 for Workstation and $XXX for Server :) –  Jun 16 '11 at 12:14
  • Here is how to do this from PowerShell: `(Get-WmiObject -class Win32_OperatingSystem).ProductType` For the possible return values, see the ProductType table on the MSDN page on [Win32_OperatingSystem class](http://msdn.microsoft.com/en-us/library/aa394239.aspx) – MarnixKlooster ReinstateMonica Nov 01 '13 at 12:00

3 Answers3

7

Ok, Alex, it looks like you can use WMI to find this out:

using System.Management;

public bool IsServerVersion()
{
    var productType = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
            .Get().OfType<ManagementObject>()
            .Select(o => (uint)o.GetPropertyValue("ProductType")).First();

    // ProductType will be one of:
    // 1: Workstation
    // 2: Domain Controller
    // 3: Server

    return productType != 1;
}

You'll need a reference to the System.Management assembly in your project.

Or the .NET 2.0 version without any LINQ-type features:

public bool IsServerVersion()
{
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
    {
        foreach (ManagementObject managementObject in searcher.Get())
        {
            // ProductType will be one of:
            // 1: Workstation
            // 2: Domain Controller
            // 3: Server
            uint productType = (uint)managementObject.GetPropertyValue("ProductType");
            return productType != 1;
        }
    }

    return false;
}
Mike Goatly
  • 7,380
  • 2
  • 32
  • 33
2

You can do this by checking the ProductType in the registry, if it is ServerNT you are on a windows server system if it is WinNT you are on a workstation.

    using Microsoft.Win32;
    String strOSProductType = Registry.GetValue("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 
                                                "ProductType", 
                                                "Key doesn't Exist" ).ToString() ;
    if( strOSProductType == "ServerNT" )
    {
        //Windows Server
    }
    else if( strOsProductType == "WinNT" )
    {
        //Windows Workstation
    }
Tommy
  • 350
  • 3
  • 11
1

There is no special flag for server windows versions, you need to check version IDs. Take a look on tables in article: http://www.codeguru.com/cpp/w-p/system/systeminformation/article.php/c8973

Anton Semenov
  • 6,227
  • 5
  • 41
  • 69
  • In fact, that very article you link to says: "The *product type* groups the operating systems in servers (3), workstations (2), and domain controllers (1)." – Heinzi Jun 16 '11 at 08:36