我也尝试了@ylax的答案,但它指向了一个C++宏,而不是你可以从C#中调用的内容。
我已经生成了这个辅助方法,希望能对某人有所帮助:
static class WindowsOperatingSystemHelper
{
[DllImport("kernel32.dll", SetLastError = true)]
[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
public static extern bool GetProductInfo(
int dwOSMajorVersion,
int dwOSMinorVersion,
int dwSpMajorVersion,
int dwSpMinorVersion,
out int pdwReturnedProductType);
public enum OperatingSystemSKU
{
UNDEFINED = 0,
ULTIMATE = 1,
HOME_BASIC = 2,
HOME_PREMIUM = 3,
ENTERPRISE = 4,
HOME_BASIC_N = 5,
BUSINESS = 6,
STANDARD_SERVER = 7,
DATACENTER_SERVER = 8,
SMALLBUSINESS_SERVER = 9,
ENTERPRISE_SERVER = 10,
STARTER = 11,
DATACENTER_SERVER_CORE = 12,
STANDARD_SERVER_CORE = 13,
ENTERPRISE_SERVER_CORE = 14,
ENTERPRISE_SERVER_IA64 = 15,
BUSINESS_N = 16,
WEB_SERVER = 17,
CLUSTER_SERVER = 18,
HOME_SERVER = 19,
STORAGE_EXPRESS_SERVER = 20,
STORAGE_STANDARD_SERVER = 21,
STORAGE_WORKGROUP_SERVER = 22,
STORAGE_ENTERPRISE_SERVER = 23,
SERVER_FOR_SMALLBUSINESS = 24,
SMALLBUSINESS_SERVER_PREMIUM = 25,
HOME_PREMIUM_N = 26,
ENTERPRISE_N = 27,
ULTIMATE_N = 28,
WEB_SERVER_CORE = 29,
MEDIUMBUSINESS_SERVER_MANAGEMENT = 30,
MEDIUMBUSINESS_SERVER_SECURITY = 31,
MEDIUMBUSINESS_SERVER_MESSAGING = 32,
SERVER_FOUNDATION = 33,
HOME_PREMIUM_SERVER = 34,
SERVER_FOR_SMALLBUSINESS_V = 35,
STANDARD_SERVER_V = 36,
DATACENTER_SERVER_V = 37,
ENTERPRISE_SERVER_V = 38,
DATACENTER_SERVER_CORE_V = 39,
STANDARD_SERVER_CORE_V = 40,
ENTERPRISE_SERVER_CORE_V = 41,
HYPERV = 42,
STORAGE_EXPRESS_SERVER_CORE = 43,
STORAGE_STANDARD_SERVER_CORE = 44,
STORAGE_WORKGROUP_SERVER_CORE = 45,
STORAGE_ENTERPRISE_SERVER_CORE = 46,
STARTER_N = 47,
PROFESSIONAL = 48,
PROFESSIONAL_N = 49,
SB_SOLUTION_SERVER = 50,
SERVER_FOR_SB_SOLUTIONS = 51,
STANDARD_SERVER_SOLUTIONS = 52,
STANDARD_SERVER_SOLUTIONS_CORE = 53,
SB_SOLUTION_SERVER_EM = 54,
SERVER_FOR_SB_SOLUTIONS_EM = 55,
SOLUTION_EMBEDDEDSERVER = 56,
SOLUTION_EMBEDDEDSERVER_CORE = 57,
PROFESSIONAL_EMBEDDED = 58,
ESSENTIALBUSINESS_SERVER_MGMT = 59,
ESSENTIALBUSINESS_SERVER_ADDL = 60,
ESSENTIALBUSINESS_SERVER_MGMTSVC = 61,
ESSENTIALBUSINESS_SERVER_ADDLSVC = 62,
SMALLBUSINESS_SERVER_PREMIUM_CORE = 63,
CLUSTER_SERVER_V = 64,
EMBEDDED = 65,
STARTER_E = 66,
HOME_BASIC_E = 67,
HOME_PREMIUM_E = 68,
PROFESSIONAL_E = 69,
ENTERPRISE_E = 70,
ULTIMATE_E = 71,
ENTERPRISE_EVALUATION = 72,
MULTIPOINT_STANDARD_SERVER = 76,
MULTIPOINT_PREMIUM_SERVER = 77,
STANDARD_EVALUATION_SERVER = 79,
DATACENTER_EVALUATION_SERVER = 80,
ENTERPRISE_N_EVALUATION = 84,
EMBEDDED_AUTOMOTIVE = 85,
EMBEDDED_INDUSTRY_A = 86,
THINPC = 87,
EMBEDDED_A = 88,
EMBEDDED_INDUSTRY = 89,
EMBEDDED_E = 90,
EMBEDDED_INDUSTRY_E = 91,
EMBEDDED_INDUSTRY_A_E = 92,
STORAGE_WORKGROUP_EVALUATION_SERVE = 95,
STORAGE_STANDARD_EVALUATION_SERVER = 96,
CORE_ARM = 97,
CORE_N = 98,
CORE_COUNTRYSPECIFIC = 99,
CORE_SINGLELANGUAGE = 100,
CORE = 101,
PROFESSIONAL_WMC = 103,
EMBEDDED_INDUSTRY_EVAL = 105,
EMBEDDED_INDUSTRY_E_EVAL = 106,
EMBEDDED_EVAL = 107,
EMBEDDED_E_EVAL = 108,
NANO_SERVER = 109,
CLOUD_STORAGE_SERVER = 110,
CORE_CONNECTED = 111,
PROFESSIONAL_STUDENT = 112,
CORE_CONNECTED_N = 113,
PROFESSIONAL_STUDENT_N = 114,
CORE_CONNECTED_SINGLELANGUAGE = 115,
CORE_CONNECTED_COUNTRYSPECIFIC = 116,
CONNECTED_CAR = 117,
INDUSTRY_HANDHELD = 118,
PPI_PRO = 119,
ARM64_SERVER = 120,
EDUCATION = 121,
EDUCATION_N = 122,
IOTUAP = 123,
CLOUD_HOST_INFRASTRUCTURE_SERVER = 124,
ENTERPRISE_S = 125,
ENTERPRISE_S_N = 126,
PROFESSIONAL_S = 127,
PROFESSIONAL_S_N = 128,
ENTERPRISE_S_EVALUATION = 129,
ENTERPRISE_S_N_EVALUATION = 130,
HOLOGRAPHIC = 135,
PRO_SINGLE_LANGUAGE = 138,
PRO_CHINA = 139,
ENTERPRISE_SUBSCRIPTION = 140,
ENTERPRISE_SUBSCRIPTION_N = 141,
DATACENTER_NANO_SERVER = 143,
STANDARD_NANO_SERVER = 144,
DATACENTER_A_SERVER_CORE = 145,
STANDARD_A_SERVER_CORE = 146,
DATACENTER_WS_SERVER_CORE = 147,
STANDARD_WS_SERVER_CORE = 148,
UTILITY_VM = 149,
DATACENTER_EVALUATION_SERVER_CORE = 159,
STANDARD_EVALUATION_SERVER_CORE = 160,
PRO_WORKSTATION = 161,
PRO_WORKSTATION_N = 162,
PRO_FOR_EDUCATION = 164,
PRO_FOR_EDUCATION_N = 165,
AZURE_SERVER_CORE = 168,
AZURE_NANO_SERVER = 169,
ENTERPRISEG = 171,
ENTERPRISEGN = 172,
SERVERRDSH = 175,
CLOUD = 178,
CLOUDN = 179,
HUBOS = 180,
ONECOREUPDATEOS = 182,
CLOUDE = 183,
ANDROMEDA = 184,
IOTOS = 185,
CLOUDEN = 186,
}
public static bool TryGetOperatingSystem(this Version version, out OperatingSystemSKU sku)
{
sku = OperatingSystemSKU.UNDEFINED;
if (GetProductInfo(version.Major, version.Minor, 0, 0, out int productType))
{
sku = (OperatingSystemSKU)productType;
}
return sku != OperatingSystemSKU.UNDEFINED;
}
public static bool IsWindowsServer()
{
if (TryGetOperatingSystem(Environment.OSVersion.Version, out var osSKU))
{
return osSKU.ToString().Contains("SERVER") || osSKU.ToString().Contains("DATA_CENTER") || osSKU == OperatingSystemSKU.CLUSTER_SERVER;
}
else
{
return false;
}
}
}
我从一个Powershell网站上得到了这个枚举,我猜当微软生成一个新的操作系统时,每隔一段时间就需要更新它。
像这样调用它:
if (WindowsOperatingSystemHelper.IsWindowsServer())
{
... your magic
}
或者如果你想知道操作系统:
if (WindowsOperatingSystemHelper.TryGetOperatingSystem(Environment.OSVersion.Version, out var operatingSystem))
{
... your magic
}