是引用Windows內建的dll去抓Mac Address
還不錯
就貼上來
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, byte[] MacAddr, ref uint PhyAddrLen);
static object[] GetRemoteMAC(IPAddress LocalIP, IPAddress RemoteIP) //抓Mac
{
byte[] MacAddr = new byte[6];
uint PhyAddrLen = (uint)MacAddr.Length;
string temp = "";
//LiveAndMAC[0]是否有回應 LiveAndMAC[1] MAC
object[] LiveAndMAC = new object[2];
int res = SendARP((int)RemoteIP.Address, (int)LocalIP.Address, MacAddr, ref PhyAddrLen);
//如果有回達 res就是0
if (res == 0)
{
for (int i = 0; i < PhyAddrLen; i++)
{
//轉成看得懂的MAC型態
temp += MacAddr[i].ToString("x2") + "-";
}
//減掉最後一個"-" 並轉成大寫 如果temp長度為0 就代表有回應但沒有MAC 以沒有回應處理
if (temp.Length != 0)
{
temp = (temp.Remove(temp.Length - 1, 1)).ToUpper();
LiveAndMAC[0] = 1;
LiveAndMAC[1] = temp;
}
else
{
LiveAndMAC[0] = 0;
LiveAndMAC[1] = "";
}
return LiveAndMAC;
}
else
{
LiveAndMAC[0] = 0;
LiveAndMAC[1] = "";
return LiveAndMAC;
}
}
PS...補充一下
IPAddress LocalIP = IPAddress.Parse("0.0.0.0"); //ip宣告的方式
沒有留言:
張貼留言