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的信息显示出来。

转载于:https://www.cnblogs.com/dancer/archive/2010/06/21/1762230.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值