您的位置:首页 > 编程语言 > C#

C#获取硬件信息

2009-03-26 11:08 267 查看
C#获取硬件信息

//经测试通过

//需引用System.Management

using System;

using System.Management;

namespace MyCustomClassLib

{

/// <summary>

/// 硬件信息类

/// </summary>

public class HardwareInfo

{

public HardwareInfo()

{

}

#region 硬件属性

/// <summary>

/// 机器名

/// </summary>

public string HostName

{

get

{return System.Net.Dns.GetHostName();

}

}

/// <summary>

/// CPU编号

/// </summary>

public string CPUID

{

get{return GetCpuID();}

}

ASP.NET获取CPU序列号,硬盘序列号ID,获取网卡编号
public static void GetCpuInfo(out string cpuInfo)
{
//得到cpu信息
string _cpuInfo="";//cpu信息
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
_cpuInfo = mo.Properties["ProcessorId"].Value.ToString();

}
cpuInfo=_cpuInfo;
}

//获取硬盘ID
string _HDInfo="";
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach(ManagementObject mo in moc1)
{
_HDInfo = (string)mo.Properties["Model"].Value;

}
HDInfo=_HDInfo;
}

public static void GetMacAddress(out string MacAddress)
{
//获取网卡硬件地址

string _MacAddress="";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach(ManagementObject mo in moc2)
{
if((bool)mo["IPEnabled"] == true)
MacAddress=mo["MacAddress"].ToString();
mo.Dispose();
}
MacAddress=_MacAddress;
}

学习WMI有感
通过两天的学习发现WMI确实是个好东西:),利用它我们可以很方便的对计算机的硬件信息进行管理,但是用它得到的硬件信息十分有限,特别适用于对硬件信息细节要求不高的用户,例如一些OEM厂商对产线上的产品进行管理和检测,有了它,从此对那些繁琐的API说声byebye了......

下面这几个小例子是我这两天的学习心得:

//Get LAN MAC address
static void GetLanMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Console.WriteLine("MAC address/t{0}", mo["MacAddress"].ToString());
mo.Dispose();
}
Console.WriteLine ("/n/r");
}
//Get HDD Size / Serial # / Firmware
static void GetHDDInfo()
{
String HDid;
ManagementClass cimobject = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
HDid = (string)mo.Properties["Model"].Value;

//Console.WriteLine("HDD Serial/t{0}", mo["Serial Number"].ToString());

Console.WriteLine("HDD Size/t{0} Bytes", mo["Size"].ToString());
Console.WriteLine("HDD Model/t{0}", mo["Model"].ToString());
Console.WriteLine("HDD Manufacturer /t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine("HDD Model/t{0}", mo["Firmware"].ToString());

mo.Dispose ();
}

Console.WriteLine ("/n/r");
}
//Get CPU INFO.
static void GetCpuInfo()
{
//string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
Console.WriteLine("ProcessorId:/t{0}", mo["ProcessorId"].ToString());
Console.WriteLine("CurrentClockSpeed:/t{0}", mo["CurrentClockSpeed"].ToString());

//Console.WriteLine("L2CacheSize:/t{0}", mo["L2CacheSize"].ToString());
//Console.WriteLine("L2CacheSpeed:/t{0}", mo["L2CacheSpeed"].ToString());

Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("ProcessorType:/t{0}", mo["ProcessorType"].ToString());
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
Console.WriteLine("Revision:/t{0}", mo["Revision"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
//Console.WriteLine(cpuInfo);
}
Console.WriteLine ("/n/r");
}
//Get Cahce INFO.
static void GetCacheInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_CacheMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("MaxCacheSize:/t{0}", mo["MaxCacheSize"].ToString());
Console.WriteLine("Location:/t{0}", mo["Purpose"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get Memory INFO.
static void GetMemoryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Capacity:/t{0}", mo["Capacity"].ToString());
Console.WriteLine("DeviceLocator:/t{0}", mo["DeviceLocator"].ToString());
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the battery INFO.
static void GetBatteryInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_Battery");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
//Console.WriteLine("Capacity:/t{0}", mo["BatteryRechargeTime"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BatteryStatus"].ToString());
Console.WriteLine("Description:/t{0}", mo["Chemistry"].ToString());
}
Console.WriteLine ("/n/r");
}
//Get the LCD INFO.
static void GetLCDInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DesktopMonitor");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("PixelsPerXLogicalInch:/t{0}", mo["PixelsPerXLogicalInch"].ToString());
Console.WriteLine("PixelsPerYLogicalInch:/t{0}", mo["PixelsPerYLogicalInch"].ToString());
Console.WriteLine("Manufacturer:/t{0}", mo["MonitorManufacturer"].ToString ()); //[]
Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get the BIOS INFO.
static void GetBIOSInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_BIOS");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Version:/t{0}", mo["Version"].ToString());
//Console.WriteLine("DeviceLocator:/t{0}", mo["BuildNumber"].ToString());
Console.WriteLine("BIOSVersion:/t{0}", mo["BIOSVersion"].ToString ()); //[]
Console.WriteLine("Manufacturer:/t{0}", mo["Manufacturer"].ToString());
Console.WriteLine("SerialNumber:/t{0}", mo["SerialNumber"].ToString());
//Console.WriteLine("InstallDate:/t{0}", (mo["InstallDate"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");
}
//Get HDD SerialNum
public static void GetHd()
{
ManagementObjectSearcher wmiSearcher = new ManagementObjectSearcher();

wmiSearcher.Query = new SelectQuery(
"Win32_DiskDrive",
"",
new string[]{"PNPDeviceID"}
);
ManagementObjectCollection myCollection = wmiSearcher.Get();
ManagementObjectCollection.ManagementObjectEnumerator em =
myCollection.GetEnumerator();
em.MoveNext();
ManagementBaseObject mo = em.Current;
string id = mo.Properties["PNPDeviceID"].Value.ToString().Trim();
Console.WriteLine("My harddisk is : " + id);
}
//Get Monitor INFO.
public static void GetMonitorInfo()
{
ManagementClass cimobject = new ManagementClass("Win32_DisplayConfiguration");
ManagementObjectCollection moc = cimobject.GetInstances();
foreach(ManagementObject mo in moc)
{
Console.WriteLine("Description:/t{0}", mo["Description"].ToString());
Console.WriteLine("DisplayFrequency:/t{0}", mo["DisplayFrequency"].ToString());
Console.WriteLine("PelsHeight:/t{0}", mo["PelsHeight"].ToString());
Console.WriteLine("PelsWidth:/t{0}", mo["PelsWidth"].ToString ()); //[]
//Console.WriteLine("MonitorType:/t{0}", mo["MonitorType"].ToString());
//Console.WriteLine("ScreenHeight:/t{0}", mo["ScreenHeight"].ToString());
//Console.WriteLine("ScreenWidth:/t{0}", mo["ScreenWidth"].ToString ());//datetime to string
}
Console.WriteLine ("/n/r");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: