以下是收集的幾種C#程序讀取MAC地址的方法,示例中是讀取所有網卡的MAC地址,如果僅需要讀取其中一個,稍作修改即可。
1 通過IPConfig命令讀取MAC地址
///
<summary>
/// 根據截取ipconfig /all命令的輸出流獲取網卡Mac
/// </summary>
/// <returns></returns>
public static List < string > GetMacByIPConfig()
{
List < string > macs = new List < string > ();
/// 根據截取ipconfig /all命令的輸出流獲取網卡Mac
/// </summary>
/// <returns></returns>
public static List < string > GetMacByIPConfig()
{
List < string > macs = new List < string > ();
ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig", "/all");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
Process p = Process.Start(startInfo);
// 截取輸出流
StreamReader reader = p.StandardOutput;
string line = reader.ReadLine();
while ( ! reader.EndOfStream)
{
if ( ! string .IsNullOrEmpty(line))
{
line = line.Trim();
if (line.StartsWith( " Physical Address " ))
{
macs.Add(line);
}
}
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
Process p = Process.Start(startInfo);
// 截取輸出流
StreamReader reader = p.StandardOutput;
string line = reader.ReadLine();
while ( ! reader.EndOfStream)
{
if ( ! string .IsNullOrEmpty(line))
{
line = line.Trim();
if (line.StartsWith( " Physical Address " ))
{
macs.Add(line);
}
}
line = reader.ReadLine();
}
// 等待程序執行完退出進程
p.WaitForExit();
p.Close();
reader.Close();
return macs;
}
2 通過WMI讀取MAC地址
1)該方法依賴WMI的系統服務,該服務一般不會被關閉;但如果系統服務缺失或者出現問題,該方法無法取得MAC地址。
///
<summary>
/// 通過WMI讀取系統信息里的網卡MAC
/// </summary>
/// <returns></returns>
public static List < string > GetMacByWMI()
{
List < string > macs = new List < string > ();
try
{
string mac = "" ;
ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (( bool )mo[ " IPEnabled " ])
{
mac = mo[ " MacAddress " ].ToString();
macs.Add(mac );
}
}
moc = null ;
mc = null ;
}
catch
{
}
return macs;
}
/// 通過WMI讀取系統信息里的網卡MAC
/// </summary>
/// <returns></returns>
public static List < string > GetMacByWMI()
{
List < string > macs = new List < string > ();
try
{
string mac = "" ;
ManagementClass mc = new ManagementClass( " Win32_NetworkAdapterConfiguration " );
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (( bool )mo[ " IPEnabled " ])
{
mac = mo[ " MacAddress " ].ToString();
macs.Add(mac );
}
}
moc = null ;
mc = null ;
}
catch
{
}
return macs;
}
3 通過NetworkInterface讀取MAC地址
1)如果當前的網卡是禁用狀態(硬件處於硬關閉狀態),取不到該網卡的MAC地址,(您可以通過禁用網卡進行試驗)。
2)如果當前啟用了多個網卡,最先返回的地址是最近啟用的網絡連接的信息
//
返回描述本地計算機上的網絡接口的對象(網絡接口也稱為網絡適配器)。
public static NetworkInterface[] NetCardInfo()
{
return NetworkInterface.GetAllNetworkInterfaces();
}
/// <summary>
/// 通過NetworkInterface讀取網卡Mac
/// </summary>
/// <returns></returns>
public static List < string > GetMacByNetworkInterface()
{
List < string > macs = new List < string > ();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
macs.Add(ni.GetPhysicalAddress().ToString());
}
return macs;
}
public static NetworkInterface[] NetCardInfo()
{
return NetworkInterface.GetAllNetworkInterfaces();
}
/// <summary>
/// 通過NetworkInterface讀取網卡Mac
/// </summary>
/// <returns></returns>
public static List < string > GetMacByNetworkInterface()
{
List < string > macs = new List < string > ();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
macs.Add(ni.GetPhysicalAddress().ToString());
}
return macs;
}
4 通過SendARP讀取MAC地址
///
<summary>
/// 通過SendARP獲取網卡Mac
/// 網絡被禁用或未接入網絡(如沒插網線)時此方法失靈
/// </summary>
/// <param name="remoteIP"></param>
/// <returns></returns>
public static string GetMacBySendARP( string remoteIP)
{
StringBuilder macAddress = new StringBuilder();
try
{
Int32 remote = inet_addr(remoteIP);
Int64 macInfo = new Int64();
Int32 length = 6 ;
SendARP(remote, 0 , ref macInfo, ref length);
string temp = Convert.ToString(macInfo, 16 ).PadLeft( 12 , ' 0 ' ).ToUpper();
int x = 12 ;
for ( int i = 0 ; i < 6 ; i ++ )
{
if (i == 5 )
{
macAddress.Append(temp.Substring(x - 2 , 2 ));
}
else
{
macAddress.Append(temp.Substring(x - 2 , 2 ) + " - " );
}
x -= 2 ;
}
return macAddress.ToString();
}
catch
{
return macAddress.ToString();
}
}
[DllImport( " Iphlpapi.dll " )]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport( " Ws2_32.dll " )]
private static extern Int32 inet_addr( string ip);
/// 通過SendARP獲取網卡Mac
/// 網絡被禁用或未接入網絡(如沒插網線)時此方法失靈
/// </summary>
/// <param name="remoteIP"></param>
/// <returns></returns>
public static string GetMacBySendARP( string remoteIP)
{
StringBuilder macAddress = new StringBuilder();
try
{
Int32 remote = inet_addr(remoteIP);
Int64 macInfo = new Int64();
Int32 length = 6 ;
SendARP(remote, 0 , ref macInfo, ref length);
string temp = Convert.ToString(macInfo, 16 ).PadLeft( 12 , ' 0 ' ).ToUpper();
int x = 12 ;
for ( int i = 0 ; i < 6 ; i ++ )
{
if (i == 5 )
{
macAddress.Append(temp.Substring(x - 2 , 2 ));
}
else
{
macAddress.Append(temp.Substring(x - 2 , 2 ) + " - " );
}
x -= 2 ;
}
return macAddress.ToString();
}
catch
{
return macAddress.ToString();
}
}
[DllImport( " Iphlpapi.dll " )]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport( " Ws2_32.dll " )]
private static extern Int32 inet_addr( string ip);
5 從注冊表讀取MAC地址
常規用戶可通過讀取注冊表項Windows Genuine Advantage獲取到物理網卡地址。
1)如果注冊表項被修改,則無法取得該MAC地址
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows Genuine Advantage