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 適易查詢分析、工作流程、內容管理及專案管理示範平台