代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
namespace cai
{
class HardwareInfo
{
/// <summary>
/// 取機器名
/// </summary>
/// <returns></returns>
public string GetHostName()
{
return System.Net.Dns.GetHostName();
}
/// <summary>
/// 取CPU編號
/// </summary>
/// <returns></returns>
public String GetCpuID()
{
try
{
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
String strCpuID = null;
foreach (ManagementObject mo in moc)
{
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
break;
}
return strCpuID;
}
catch
{
return "";
}
}
/// <summary>
/// 讀取Mac地址
/// </summary>
/// <returns></returns>
public string GetNetCardMacAddress()
{
ManagementClass mc;
ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
str = mo["MacAddress"].ToString();
}
return str;
}
/// <summary>
/// 讀取C盤序號
/// </summary>
/// <returns></returns>
public string GetDiskVolumeSerialNumber()
{
ManagementObject disk;
disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
return disk.GetPropertyValue("VolumeSerialNumber").ToString();
}
}
}
從網路收集整理出來。。