日期:2014-05-17 浏览次数:20723 次
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int32 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
string IPAddress=System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList[0].ToString();
MessageBox.Show("ip: "+IPAddress);
string mac = this.GetMACFromIP(IPAddress);
MessageBox.Show("mac: "+mac);
private string GetMACFromIP(string A_strIP)
{
string strRet = "Unknown";
string strIPPattern = @"^\d+\.\d+\.\d+\.\d+$";
Regex objRex =new Regex(strIPPattern);
if(objRex.IsMatch(A_strIP) == true)
{
Int32 intDest = inet_addr(A_strIP);
Int32 [] arrMAC = new Int32[2];
Int32 intLen = 6;
int intResult = SendARP(intDest,0,ref arrMAC[0],ref intLen);
if(intResult == 0)
{
Byte [] arrbyte = new Byte[8];
arrbyte[5] = (Byte)(arrMAC[1] >> 8);
arrbyte[4] = (Byte)arrMAC[1];