您也许可以使用 WMI 来获得结果。特此链接到通过网络设备跟踪的 WMI 解决方案。
我在这里发布代码以防网站关闭,但所有功劳归于原作者 PsychoCoder。
在 C# 中使用 WMI 获取 MAC 地址
和代码:
//Namespace reference
using System.Management;
/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//af the network adapter
ManagementClass mgmt = new ManagementClass("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string address = String.Empty;
//My modification to the code
var description = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (address == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
if ((bool)obj["IPEnabled"] == true)
{
address = obj["MacAddress"].ToString();
description = obj["Description"].ToString();
}
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
address = address.Replace(":", "");
//return the mac address
return address;
}
请务必包含对 System.Management 的引用。为了让您获得网络设备名称,您可以使用obj["Description"].ToString();
您还可以查看有关 WMI 的 MSDN,特别是Win32_NetworkAdapterConfiguration 类
希望这可以帮助。