伊莉空間
小程式:顯示出網卡的 MAC
參考看看吧!
using System.Net.NetworkInformation;
private void GetMACaddress()
{
int n = 0;
int index = 0;
NetworkInterface[] m_pNics = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface m_pAdapter = null;
try
{
for (index = 0; index < m_pNics.Length; index++)
{
m_pAdapter = (NetworkInterface)m_pNics.GetValue(index);
if(m_pAdapter.Description.Contains("網卡的辨別名稱"))
{
IPInterfaceProperties m_pAdapterProperties = m_pAdapter.GetIPProperties();
// Print MAC address
string macaddr = m_pAdapter.GetPhysicalAddress().ToString();
string strMac = macaddr.Substring(0, 2);
for (n = 0; n < 5; n++)
strMac += ":" + macaddr.Substring(2 * n + 2, 2);
//strMac is MAC address
m_pAdapterProperties = null;
}
else
{
}
m_pAdapter = null;
} //for (index = 0; index < m_pNics.Length; index++)
m_pNics = null;
}
catch (Exception)
{
m_pNics = null;
m_pAdapter = null;
}
} |