我正在使用wmi调用以获取如下所示的信息
var queryObj = new ObjectQuery("SELECT * FROM Win32_Processor");
var vmSearcher = new ManagementObjectSearcher(queryObj);
foreach (ManagementObject MO in vmSearcher.Get())
{
if (MO.GetType().GetProperty("AddressWidth") != null)
{
Value = MO["AddressWidth"].ToString();
}
//TRY TO FORCE TO NOT EXIST TO TEST..IS THIS THE WAY TO FORCE A
//PROPERTY OUT??
MO["CurrentClockSpeed"] = null;
if (MO.GetType().GetProperty("CurrentClockSpeed") != null)
{
Value2 = MO["CurrentClockSpeed"].ToString();
}
}
问题是某些机器具有某些属性,而另一些具有其他属性
如何检查计算机上是否存在属性? 我的东西没用
我最终想要的是简单地打印出我选择的属性(例如代码示例中的on)(如果存在)
答案 0 :(得分:1)
public static object TryGetProperty(ManagementObject wmiObj, string propertyName)
{
object retval;
try
{
retval = wmiObj.GetPropertyValue(propertyName);
}
catch (System.Management.ManagementException ex)
{
retval = null;
}
return retval;
}