c# – 如何区分服务器版本和Windows客户端版本?

前端之家收集整理的这篇文章主要介绍了c# – 如何区分服务器版本和Windows客户端版本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何区分服务器版本和 Windows客户端版本?
示例:XP,Vista,7 vs Win2003,Win2008.

UPD:需要一个方法,如

bool IsServerVersion()
{
    return ...;
}

解决方法

@H_403_10@ 好的,Alex,看起来你可以使用WMI找到它:
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;
}

您需要在项目中引用System.Management程序集.

或者没有任何LINQ类型功能的.NET 2.0版本:

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;
}
原文链接:https://www.f2er.com/csharp/244097.html

猜你在找的C#相关文章