Search code examples
c#.net-2.0

How to distinguish the server version from the client version of Windows?


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 ...;
}

Solution

  • 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;
    }