c#通过WMI获取硬件资源代码:
public class GetSystemInfo
{
private ManagementClass mc;
private ManagementObjectCollection moc;
public GetSystemInfo()
{ }
public string GetHardDiskInfo()
{
mc = new ManagementClass("Win32_DiskDrive");
moc = mc.GetInstances();
String HardDiskInfo = string.Empty;
foreach (ManagementObject mo in moc)
{
HardDiskInfo = "硬盘类型:" + mo.Properties["Manufacturer"].Value.ToString() + "\n" +
" 硬盘型号:" + mo.Properties["Model"].Value.ToString() + "\n" +
" 硬盘容量:" + mo.Properties["Size"].Value.ToString() + "\n" +
" 硬盘序列号:" + mo.Properties["PNPDeviceID"].Value.ToString() + "\n" +
" 硬盘分区数:" + mo.Properties["Partitions"].Value.ToString() + "\n" +
" 字节/扇:" + mo.Properties["BytesPerSector"].Value.ToString() + "\n" +
" 扇区/道:" + mo.Properties["SectorsPerTrack"].Value.ToString() + "\n" +
" 磁道/族:" + mo.Properties["TracksPerCylinder"].Value.ToString() + "\n" +
" 总扇区:" + mo.Properties["TotalSectors"].Value.ToString() + "\n" +
" 总磁道:" + mo.Properties["TotalTracks"].Value.ToString() + "\n" +
" 总族数:" + mo.Properties["TotalCylinders"].Value.ToString() + "\n" +
" 总磁头:" + mo.Properties["TotalHeads"].Value.ToString();
}
moc = null;
mc = null;
return HardDiskInfo;
}
public string GetBIOSInfo()
{
mc = new ManagementClass("Win32_BIOS");
moc = mc.GetInstances();
String BIOSInfo = null;
foreach (ManagementObject mo in moc)
{
BIOSInfo = "BIOS厂商:" + mo.Properties["Manufacturer"].Value.ToString() + "\n" +
" BIOS版本:" + mo.Properties["Version"].Value.ToString();
}
moc = null;
mc = null;
return BIOSInfo;
}
public string GetVideoCardInfo()
{
mc = new ManagementClass("Win32_VideoController");
moc = mc.GetInstances();
String VideoCardInfo = string.Empty;
foreach (ManagementObject mo in moc)
{
foreach (PropertyData pd in mo.Properties)
{
if (pd.Name.Equals("Name"))
{
VideoCardInfo = VideoCardInfo + " 显卡名称:" + pd.Value + "\n";
}
else if (pd.Name.Equals("PNPDeviceID"))
{
VideoCardInfo = VideoCardInfo + " 显卡序列号:" + pd.Value + "\n";
}
else if (pd.Name.Equals("DeviceID"))
{
VideoCardInfo = VideoCardInfo + " 显卡ID:" + pd.Value + "\n";
}
else if (pd.Name.Equals("Status"))
{
VideoCardInfo = VideoCardInfo + " 显卡状态:" + pd.Value + "\n";
}
else if (pd.Name.Equals("VideoModeDescription"))
{
VideoCardInfo = VideoCardInfo + " 显卡运行模式:" + pd.Value;
}
}
}
moc = null;
mc = null;
return VideoCardInfo;
}
public string GetUSBInfo()
{
mc = new ManagementClass("Win32_USBController");
moc = mc.GetInstances();
String USBInfo = string.Empty;
foreach (ManagementObject mo in moc)
{
foreach (PropertyData pd in mo.Properties)
{
if (pd.Name.Equals("Manufacturer"))
{
USBInfo = USBInfo + " 厂商:" + pd.Value + "\n";
}
else if (pd.Name.Equals("Name"))
{
USBInfo = USBInfo + " USB控制器名称:" + pd.Value + "\n";
}
else if (pd.Name.Equals("Status"))
{
USBInfo = USBInfo + " USB状态:" + pd.Value + "\n";
}
else if (pd.Name.Equals("PNPDeviceID"))
{
USBInfo = USBInfo + " USB控制器ID:" + pd.Value;
}
}
}
moc = null;
mc = null;
return USBInfo;
}
public string GetCPUInfo()
{
mc = new ManagementClass("Win32_Processor");
moc = mc.GetInstances();
String CPUInfo = string.Empty;
foreach (ManagementObject mo in moc)
{
CPUInfo = "CPU频率:" + mo.Properties["currentClockSpeed"].Value.ToString() + "\n" +
" CPU序号:" + mo.Properties["ProcessorID"].Value.ToString() + "\n" +
" CPU主频:" + mo.Properties["ExtClock"].Value.ToString() + "\n" +
" 32位数据宽度:" + mo.Properties["AddressWidth"].Value.ToString() + "\n" +
" 32位型号描述:" + mo.Properties["Description"].Value.ToString() + "\n" +
" CPU厂商:" + mo.Properties["Manufacturer"].Value.ToString();
}
moc = null;
mc = null;
return CPUInfo;
}
public string GetMemoryInfo()
{
mc = new ManagementClass("Win32_PhysicalMemory");
moc = mc.GetInstances();
String MemoryInfo = string.Empty;
foreach (ManagementObject mo in moc)
{
MemoryInfo = "内存名:" + mo.Properties["Name"].Value.ToString() + "\n" +
//" 内存序列号:" + mo.Properties["PartNumber"].Value.ToString() + "\n" +
//" Version:" + mo.Properties["Version"].Value.ToString() + "\n" +
" TotalWidth:" + mo.Properties["TotalWidth"].Value.ToString();
}
moc = null;
mc = null;
return MemoryInfo;
}
}