用.net语言写一个获取cpu、主板、硬盘、网卡序列号和mac地址的类
时间: 2023-11-17 16:06:05 浏览: 145
以下是一个使用 .NET 语言(C#)编写的获取计算机硬件信息的类:
```csharp
using System;
using System.Management;
public class HardwareInfo
{
public string GetCpuId()
{
string cpuId = "";
ManagementClass mc = new ManagementClass("win32_processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
cpuId = mo.Properties["processorID"].Value.ToString();
break;
}
return cpuId;
}
public string GetMotherboardId()
{
string motherboardId = "";
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
motherboardId = mo.Properties["SerialNumber"].Value.ToString();
break;
}
return motherboardId;
}
public string GetHardDiskId()
{
string hardDiskId = "";
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
hardDiskId = (string)mo["SerialNumber"];
break;
}
return hardDiskId;
}
public string GetMACAddress()
{
string macAddress = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
macAddress = mo["MacAddress"].ToString();
break;
}
}
return macAddress;
}
}
```
使用方法:
```csharp
HardwareInfo hwInfo = new HardwareInfo();
string cpuId = hwInfo.GetCpuId();
string motherboardId = hwInfo.GetMotherboardId();
string hardDiskId = hwInfo.GetHardDiskId();
string macAddress = hwInfo.GetMACAddress();
```
阅读全文