今天我们向您展示如何使用 C# 获取计算机硬件信息。您可以获取您的系统信息,如处理器 ID、硬盘序列号、系统 MAC 地址、主板制造商、主板产品 ID、CD-DVD 驱动器路径、BIOS 制造商、BIOS 序列号、BIOS 标题、系统帐户名称、物理 RAM 内存, 主板上的 RAM 插槽编号, CPU 制造商, CPU 的当前时钟速度, 默认 IP 网关, CPU 速度, 获取当前语言, 系统信息, 处理器信息, 获取计算机名称。
如果您熟悉 Windows 窗体,那么您非常了解如何创建 Windows 窗体应用程序。
了解更多:如何在 VISUAL STUDIO 2010 中创建 WINDOWS 窗体应用程序
添加“ System.Management ”的引用以使用C#获取计算机硬件信息。
请参见下图以在项目中添加参考。
添加参考
添加“System.Management”的引用
您可能会阅读:将参数从一个窗口传递到另一个窗口 c#
在您的项目中将类文件名添加为“ HardwareInfo.cs ”。在您的类文件中添加以下代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
public static class HardwareInfo
{
///
/// Retrieving Processor Id.
///
///
///
public static String GetProcessorId()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String Id = String.Empty;
foreach (ManagementObject mo in moc)
{
Id = mo.Properties["processorID"].Value.ToString();
break;
}
return Id;
}
///
/// Retrieving HDD Serial No.
///
///
public static String GetHDDSerialNo()
{
ManagementClass mangnmt = new ManagementClass("Win32_LogicalDisk");
ManagementObjectCollection mcol = mangnmt.GetInstances();
string result = "";
foreach (ManagementObject strt in mcol)
{
result += Convert.ToString(strt["VolumeSerialNumber"]);
}
return result;
}
///
/// Retrieving System MAC Address.
///
///
public static string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty)
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
///
/// Retrieving Motherboard Manufacturer.
///
///
public static string GetBoardMaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "Board Maker: Unknown";
}
///
/// Retrieving Motherboard Product Id.
///
///
public static string GetBoardProductId()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BaseBoard");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Product").ToString();
}
catch { }
}
return "Product: Unknown";
}
///
/// Retrieving CD-DVD Drive Path.
///
///
public static string GetCdRomDrive()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Drive").ToString();
}
catch { }
}
return "CD ROM Drive Letter: Unknown";
}
///
/// Retrieving BIOS Maker.
///
///
public static string GetBIOSmaker()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Manufacturer").ToString();
}
catch { }
}
return "BIOS Maker: Unknown";
}
///
/// Retrieving BIOS Serial No.
///
///
public static string GetBIOSserNo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("SerialNumber").ToString();
}
catch { }
}
return "BIOS Serial Number: Unknown";
}
///
/// Retrieving BIOS Caption.
///
///
public static string GetBIOScaption()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Caption").ToString();
}
catch { }
}
return "BIOS Caption: Unknown";
}
///
/// Retrieving System Account Name.
///
///
public static string GetAccountName()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_UserAccount");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("Name").ToString();
}
catch { }
}
return "User Account Name: Unknown";
}
///
/// Retrieving Physical Ram Memory.
///
///
public static string GetPhysicalMemory()
{
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oCollection = oSearcher.Get();
long MemSize = 0;
long mCap = 0;
// In case more than one Memory sticks are installed
foreach (ManagementObject obj in oCollection)
{
mCap = Convert.ToInt64(obj["Capacity"]);
MemSize += mCap;
}
MemSize = (MemSize / 1024) / 1024;
return MemSize.ToString() + "MB";
}
///
/// Retrieving No of Ram Slot on Motherboard.
///
///
public static string GetNoRamSlots()
{
int MemSlots = 0;
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery2 = new ObjectQuery("SELECT MemoryDevices FROM Win32_PhysicalMemoryArray");
ManagementObjectSearcher oSearcher2 = new ManagementObjectSearcher(oMs, oQuery2);
ManagementObjectCollection oCollection2 = oSearcher2.Get();
foreach (ManagementObject obj in oCollection2)
{
MemSlots = Convert.ToInt32(obj["MemoryDevices"]);
}
return MemSlots.ToString();
}
//Get CPU Temprature.
///
/// method for retrieving the CPU Manufacturer
/// using the WMI class
///
/// CPU Manufacturer
public static string GetCPUManufacturer()
{
string cpuMan = String.Empty;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuMan == String.Empty)
{
// only return manufacturer from first CPU
cpuMan = obj.Properties["Manufacturer"].Value.ToString();
}
}
return cpuMan;
}
///
/// method to retrieve the CPU's current
/// clock speed using the WMI class
///
/// Clock speed
public static int GetCPUCurrentClockSpeed()
{
int cpuClockSpeed = 0;
//create an instance of the Managemnet class with the
//Win32_Processor class
ManagementClass mgmt = new ManagementClass("Win32_Processor");
//create a ManagementObjectCollection to loop through
ManagementObjectCollection objCol = mgmt.GetInstances();
//start our loop for all processors found
foreach (ManagementObject obj in objCol)
{
if (cpuClockSpeed == 0)
{
// only return cpuStatus from first CPU
cpuClockSpeed = Convert.ToInt32(obj.Properties["CurrentClockSpeed"].Value.ToString());
}
}
//return the status
return cpuClockSpeed;
}
///
/// method to retrieve the network adapters
/// default IP gateway using WMI
///
/// adapters default IP gateway
public static string GetDefaultIPGateway()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//of the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string gateway = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (gateway == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
//check to see if the adapter's IPEnabled
//equals true
if ((bool)obj["IPEnabled"] == true)
{
gateway = obj["DefaultIPGateway"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
gateway = gateway.Replace(":", "");
//return the mac address
return gateway;
}
///
/// Retrieve CPU Speed.
///
///
public static double? GetCpuSpeedInGHz()
{
double? GHz = null;
using (ManagementClass mc = new ManagementClass("Win32_Processor"))
{
foreach (ManagementObject mo in mc.GetInstances())
{
GHz = 0.001 * (UInt32)mo.Properties["CurrentClockSpeed"].Value;
break;
}
}
return GHz;
}
///
/// Retrieving Current Language
///
///
public static string GetCurrentLanguage()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_BIOS");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return wmi.GetPropertyValue("CurrentLanguage").ToString();
}
catch { }
}
return "BIOS Maker: Unknown";
}
///
/// Retrieving Current Language.
///
///
public static string GetOSInformation()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject wmi in searcher.Get())
{
try
{
return ((string)wmi["Caption"]).Trim() + ", " + (string)wmi["Version"] + ", " + (string)wmi["OSArchitecture"];
}
catch { }
}
return "BIOS Maker: Unknown";
}
///
/// Retrieving Processor Information.
///
///
public static String GetProcessorInformation()
{
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
string name = (string)mo["Name"];
name = name.Replace("(TM)", "™").Replace("(tm)", "™").Replace("(R)", "®").Replace("(r)", "®").Replace("(C)", "©").Replace("(c)", "©").Replace(" ", " ").Replace(" ", " ");
info = name + ", " + (string)mo["Caption"] + ", " + (string)mo["SocketDesignation"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
///
/// Retrieving Computer Name.
///
///
public static String GetComputerName()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
String info = String.Empty;
foreach (ManagementObject mo in moc)
{
info = (string)mo["Name"];
//mo.Properties["Name"].Value.ToString();
//break;
}
return info;
}
}
请参见下图设计您的 Windows 窗体并生成按钮单击事件以使用 C# 获取计算机硬件信息。
设计形式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HardwareInformation
{
public partial class HardwareInfo_CodeScratcher : Form
{
public HardwareInfo_CodeScratcher()
{
InitializeComponent();
}
private void HardwareInfo_CodeScratcher_Load(object sender, EventArgs e)
{
}
private void ProcId_Click(object sender, EventArgs e)
{
lblPid.Text = HardwareInfo.GetProcessorId();
}
private void HDDSNo_Click(object sender, EventArgs e)
{
lblHDD.Text = HardwareInfo.GetHDDSerialNo();
}
private void BoardMake_Click(object sender, EventArgs e)
{
lblBM.Text = HardwareInfo.GetBoardMaker();
}
private void BIOSMkr_Click(object sender, EventArgs e)
{
lblBios.Text = HardwareInfo.GetBIOSmaker();
}
private void PhysicalMem_Click(object sender, EventArgs e)
{
lblPM.Text = HardwareInfo.GetPhysicalMemory();
}
private void CPUSpeed_Click(object sender, EventArgs e)
{
lblCS.Text = HardwareInfo.GetCpuSpeedInGHz().ToString();
}
private void CPUMkr_Click(object sender, EventArgs e)
{
lblCM.Text = HardwareInfo.GetCPUManufacturer();
}
}
}
硬件信息 - 输出