在.NET的项目中,有时候需要获取计算机的硬件的相关信息,在C#语言中需要利用ManagementClass这个类来进行相关操作。
现在先来介绍一下ManagementClass类,首先看一下类的继承结构:
现在看一下MSDN对ManagementClass类的解释,对表示一个通用信息模型 (CIM) 管理类。 管理类是 WMI 类,如 Win32_LogicalDisk, ,该类型可表示一个磁盘驱动器,并 Win32_Process, ,它表示的进程 Notepad.exe 等。 此类的成员可以访问 WMI 数据,使用一个特定的 WMI 类路径。
一. 接下来我们来看一下ManagementClass类中一些较为常用的方法的源码:
1.GetInstances():此方法存在四个重载
2.Get():
3.GetSubclasses():此方法存在四个重载版本
C# 提供了ManagementClass类来获取本机的一些基本信息,比如CPU的个数,CPU的频率,网卡的MAC,内存的大小,硬盘的大小等。
获取本机MAC地址:
/// <summary> /// 获取本机的MAC地址 /// </summary> /// <returns></returns> public static string GetMACString() { ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mo = mAdapter.GetInstances(); foreach (ManagementBaseObject m in mo) { if ((bool)m["IpEnabled"] == true) { return m["MacAddress"].ToString(); } } mo.Dispose(); return null; }
获取本机CPU的个数:
/// <summary> /// 获取CPU的个数 /// </summary> /// <returns></returns> public static int GetCpuCount() { try { using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) { ManagementObjectCollection cpus = mCpu.GetInstances(); return cpus.Count; } } catch { } return -1; }
获取本机CPU的频率
/// <summary> /// 获取CPU的频率 这里之所以使用string类型的数组,主要是因为cpu的多核 /// </summary> /// <returns></returns> public static string[] GetCpuMHZ() { ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection cpus = mc.GetInstances(); string[] mHz = new string[cpus.Count]; int c = 0; ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); foreach (ManagementObject mo in mySearch.Get()) { mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); c++; } mc.Dispose(); mySearch.Dispose(); return mHz; }
获取本机硬盘的大小
/// <summary> /// 获取硬盘的大小 /// </summary> /// <returns></returns> public static string GetSizeOfDisk() { ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moj = mc.GetInstances(); foreach (ManagementObject m in moj) { return m.Properties["Size"].Value.ToString(); } return "-1"; }
获取本机内存的大小:
/// <summary> /// 获取内存的大小 /// </summary> /// <returns></returns> public static string GetSizeOfMemery() { ManagementClass mc = new ManagementClass("Win32_OperatingSystem"); ManagementObjectCollection moc = mc.GetInstances(); double sizeAll = 0.0; foreach (ManagementObject m in moc) { if (m.Properties["TotalVisibleMemorySize"].Value != null) { sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString()); } } mc = null; moc.Dispose(); return sizeAll.ToString(); }
本程序是使用VS2005编写的,可运行。
ManageMentClass 对象被实例化时,根据初始化参数的不同能够获取不同的本地信息:"Win32_NetworkAdapterConfiguration" 获取本机网络适配器对象。据此可以获取网络地址等。"Win32_DiskDiver" 获取本机硬盘的相关信息,"Win32_Processor" 获取本机CPU相关的信息。"Win32_OperatingSystem" 获取内存等相关信息。
ManagementClass类的部分应用:
/// <summary> /// 获得CPU编号 /// </summary> /// <returns></returns> public string GetCpuid() { var cpuid = string.Empty; var mc = new ManagementClass("Win32_Processor"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; cpuid = mo.Properties["ProcessorId"].Value.ToString(); } return cpuid; } /// <summary> /// 获取硬盘序列号 /// </summary> /// <returns></returns> public string GetDiskSerialNumber() { //这种模式在插入一个U盘后可能会有不同的结果,如插入我的手机时 var hDid = string.Empty; var mc = new ManagementClass("Win32_DiskDrive"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; hDid = (string)mo.Properties["Model"].Value; //这名话解决有多个物理盘时产生的问题,只取第一个物理硬盘 break; } return hDid; } /// <summary> /// 获取网卡硬件地址 /// </summary> /// <returns></returns> public string GetMacAddress() { var mac = ""; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; if (!(bool) mo["IPEnabled"]) continue; mac = mo["MacAddress"].ToString(); break; } return mac; } /// <summary> /// 获取IP地址 /// </summary> /// <returns></returns> public string GetIpAddress() { var st = string.Empty; var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; if (!(bool) mo["IPEnabled"]) continue; var ar = (Array)(mo.Properties["IpAddress"].Value); st = ar.GetValue(0).ToString(); break; } return st; } /// <summary> /// 操作系统的登录用户名 /// </summary> /// <returns></returns> public string GetUserName() { return Environment.UserName; } /// <summary> /// 获取计算机名 /// </summary> /// <returns></returns> public string GetComputerName() { return Environment.MachineName; } /// <summary> /// 操作系统类型 /// </summary> /// <returns></returns> public string GetSystemType() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; st = mo["SystemType"].ToString(); } return st; } /// <summary> /// 物理内存 /// </summary> /// <returns></returns> public string GetPhysicalMemory() { var st = string.Empty; var mc = new ManagementClass("Win32_ComputerSystem"); var moc = mc.GetInstances(); foreach (var o in moc) { var mo = (ManagementObject) o; st = mo["TotalPhysicalMemory"].ToString(); } return st; } /// <summary> /// 显卡PNPDeviceID /// </summary> /// <returns></returns> public string GetVideoPnpid() { var st = ""; var mos = new ManagementObjectSearcher("Select * from Win32_VideoController"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["PNPDeviceID"].ToString(); } return st; } /// <summary> /// 声卡PNPDeviceID /// </summary> /// <returns></returns> public string GetSoundPnpid() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["PNPDeviceID"].ToString(); } return st; } /// <summary> /// CPU版本信息 /// </summary> /// <returns></returns> public string GetCpuVersion() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["Version"].ToString(); } return st; } /// <summary> /// CPU名称信息 /// </summary> /// <returns></returns> public string GetCpuName() { var st = string.Empty; var driveId = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in driveId.Get()) { var mo = (ManagementObject) o; st = mo["Name"].ToString(); } return st; } /// <summary> /// CPU制造厂商 /// </summary> /// <returns></returns> public string GetCpuManufacturer() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_Processor"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["Manufacturer"].ToString(); } return st; } /// <summary> /// 主板制造厂商 /// </summary> /// <returns></returns> public string GetBoardManufacturer() { var query = new SelectQuery("Select * from Win32_BaseBoard"); var mos = new ManagementObjectSearcher(query); var data = mos.Get().GetEnumerator(); data.MoveNext(); var board = data.Current; return board.GetPropertyValue("Manufacturer").ToString(); } /// <summary> /// 主板编号 /// </summary> /// <returns></returns> public string GetBoardId() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["SerialNumber"].ToString(); } return st; } /// <summary> /// 主板型号 /// </summary> /// <returns></returns> public string GetBoardType() { var st = string.Empty; var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); foreach (var o in mos.Get()) { var mo = (ManagementObject) o; st = mo["Product"].ToString(); } return st; }
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛