C#封装获得机器(CPU、硬盘、BIOS、MAC地址)信息

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Management;

namespace MyQuery.Utils
{
/// <summary>
/// 封装获得机器信息
/// by 贾世义 2011-4-26
/// </summary>
public static class MachineHelper
{
///<summary>
///return Volume Serial Number from hard drive
///</summary>
///<param name="strDriveLetter">[optional] Drive letter</param>
///<returns>[string] VolumeSerialNumber</returns>
public static string GetVolumeSerial(string strDriveLetter)
{
if (strDriveLetter == "" || strDriveLetter == null) strDriveLetter = "C";
ManagementObject disk =
new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter + ":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();
}

///<summary>
///Returns MAC Address from first Network Card in Computer
///</summary>
///<returns>[string] MAC Address</returns>
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) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true)
{
MACAddress = mo["MacAddress"].ToString();
}
}
else
{
break;
}
mo.Dispose();
}
MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
///<summary>
///Return processorId from first CPU in machine
///</summary>
///<returns>[string] ProcessorId</returns>
public static string GetCPUId()
{
string cpuInfo = String.Empty;
string temp = String.Empty;
ManagementClass mc = new ManagementClass("Win32_Processor");
try
{
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (cpuInfo == String.Empty)
{// only return cpuInfo from first CPU
cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
}
else
{
break;
}
}
}
catch
{
cpuInfo = AppDomain.CurrentDomain.BaseDirectory;
}
finally
{
mc.Dispose();
}

return cpuInfo;
}

///<summary>
///Return processorId from first CPU in machine
///</summary>
///<returns>[string] ProcessorId</returns>
public static string GetBIOSNumber()
{
ManagementClass mc = new ManagementClass("Win32_BIOS");

ManagementObjectCollection moc = mc.GetInstances();
string sn = String.Empty;
foreach (ManagementObject mo in moc)
{
if (String.Empty.Equals(sn))
{
sn = mo.Properties["SerialNumber"].Value.ToString();
}
else
{
break;
}
}
mc.Dispose();
moc.Dispose();
return sn;
}
}
}

欢迎访问:http://121.18.78.216 适易查询分析、工作流、内容管理及项目管理演示平台

你可能感兴趣的:(ios)