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

冷暖自知 提交于 2020-01-02 03:49:05

问题


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:


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



回答2:


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




回答3:


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
    }


来源:https://stackoverflow.com/questions/6368370/how-to-distinguish-the-server-version-from-the-client-version-of-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!