gpt4 book ai didi

c# - 获取文化(语言环境)独立的 WMI 属性

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:12 26 4
gpt4 key购买 nike

我需要获取使用 WMI 的机器的电源设置。我正在使用 MSDN Link以获得所需的值。我需要返回列表中的特定值。以下 C# 代码执行此操作:

string NamespacePath = @"root\cimv2\power";
string powerPlanClass = "Win32_powerplan";
string powerSettingClass = "Win32_PowerSettingDataIndex";

ManagementClass powerPlanManagementClass = new ManagementClass(NamespacePath + ":" + powerPlanClass);
ManagementObject powerPlanManagementObject = null;
foreach (ManagementObject managementObject in powerPlanManagementClass.GetInstances())
{
if (managementObject["IsActive"] != null && Boolean.Parse(managementObject["IsActive"].ToString()))
{
powerPlanManagementObject = managementObject;
}
}

Dictionary<string, PowerSetting> powerItems = new Dictionary<string, PowerSetting>()
{
{"AC", new PowerSetting() },
{"DC", new PowerSetting() }
};


foreach (ManagementObject oObject in powerPlanManagementObject.GetRelated(powerSettingClass))
{
var instanceId = oObject["instanceId"];
string[] powerSourceSettings = instanceId.ToString().Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
var powerSourceType = powerSourceSettings[2];
ManagementObjectCollection managementObjects = oObject.GetRelated("Win32_PowerSetting");
var elementName = string.Empty;
foreach (var managementObject in managementObjects)
{
elementName = managementObject["ElementName"].ToString();
}
var indexValue = uint.Parse(oObject["settingindexvalue"].ToString());

if (elementName.Equals("Hibernate after",StringComparison.OrdinalIgnoreCase))
{
if (powerSourceType.Equals("AC", StringComparison.Ordinal))
{
powerItems["AC"].HibernateAfter = indexValue;
}
}
}

注意与字符串“Hibernate after”的匹配。这适用于具有 en-us 语言环境的机器,但不适用于不同的语言环境。有没有其他方法可以独立于机器的区域设置获取 WMI 属性?

最佳答案

可能您可以在 Win32_PowerSettingInstanceID 中使用 GUID类而不是 ElementName。在我的机器上,它是 Microsoft:PowerSetting\{9d7815a6-7ee4-497e-8888-515a05f02364}。虽然我找不到官方文档,但这个 GUID,9d7815a6-7ee4-497e-8888-515a05f02364 似乎在某些 Windows 版本和语言环境中很常见,作为“hibernate after”或“hibernate idle”的标识符".

关于c# - 获取文化(语言环境)独立的 WMI 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com