如何区分服务器版本和Windows客户端版本?示例:XP,Vista,7 vs Win2003,Win2008.
UPD:需要一个方法,如
bool IsServerVersion()
{
return ...;
}
Run Code Online (Sandbox Code Playgroud)
好的,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;
}
Run Code Online (Sandbox Code Playgroud)
您需要在项目中引用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;
}
Run Code Online (Sandbox Code Playgroud)