WMI介绍及简单实际运用(二,Win32_Processor内容)
下面是获取计算机的CPU利用率信息:
ConnectionOptions Conn = new ConnectionOptions();
Conn.EnablePrivileges = true;
//如果是登陆其他电脑就需要提供用户名和密码
//Conn.Username = "administrator";
//Conn.Password = "";
System.Management.ManagementScope Ms = new System.Management.ManagementScope("\\\\localhost\\root\\cimv2", Conn);
System.Management.ObjectQuery Query = new System.Management.ObjectQuery("select * from Win32_Processor ");
ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Ms,Query);
WMI是支持以SQL语句的方式来查询,对于有SQL语句操作经验的开发人员来说基本上不用学习;只是根据需要的情况查询相关的WMI Classe就可以了。如果需要条件过虑的情况下直接套用where,对于WMI Class的相关成员可以从MSDN得到相关详细的资料。
Win32_Processor成员描述:
class Win32_Processor : CIM_Processor{ uint16 AddressWidth; uint16 Architecture; uint16 Availability; string Caption; uint32 ConfigManagerErrorCode; boolean ConfigManagerUserConfig; uint16 CpuStatus; string CreationClassName; uint32 CurrentClockSpeed; uint16 CurrentVoltage; uint16 DataWidth; string Description; string DeviceID; boolean ErrorCleared; string ErrorDescription; uint32 ExtClock; uint16 Family; datetime InstallDate; uint32 L2CacheSize; uint32 L2CacheSpeed; uint32 L3CacheSize; uint32 L3CacheSpeed; uint32 LastErrorCode; uint16 Level; uint16 LoadPercentage; string Manufacturer; uint32 MaxClockSpeed; string Name; uint32 NumberOfCores; uint32 NumberOfLogicalProcessors; string OtherFamilyDescription; string PNPDeviceID; uint16 PowerManagementCapabilities[]; boolean PowerManagementSupported; string ProcessorId; uint16 ProcessorType; uint16 Revision; string Role; string SocketDesignation; string Status; uint16 StatusInfo; string Stepping; string SystemCreationClassName; string SystemName; string UniqueId; uint16 UpgradeMethod; string Version; uint32 VoltageCaps;
};
可以通过LoadPercentage成员获取CPU的利用率。
ManagementObjectCollection ReturnCollection = Searcher.Get();
foreach(ManagementObject item in ReturnCollection)
{
Console.WriteLine(item["LoadPercentage"]);
}
查询会返回一个对象集,因为实际情况可能有多个CPU;因此通过遍历的方式来把具体CPU的信息显示出来。
浙公网安备 33010602011771号