獲取設備網卡的MAC地址和硬盤的卷標號
代碼
using System;
using System.Management;
namespace NetCard
{
///
/// 讀取設備
///
public class run
{
private ManagementClass mc;
private ManagementObjectCollection moc;
private ManagementObject disk;
public run( )
{}
///
/// 取得設備網卡的MAC地址
///
public string GetNetCardMacAddress( )
{
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;
}
///
/// 取得設備硬盤的卷標號
///
///
public string GetDiskVolumeSerialNumber( )
{
mc = new ManagementClass( "Win32_NetworkAdapterConfiguration" );
disk = new ManagementObject( "win32_logicaldisk.deviceid=\"c:\"" );
disk.Get( );
return disk.GetPropertyValue( "VolumeSerialNumber" ).ToString( );
}
}
}