Friday, February 8, 2008

Code to find MAC Address in ASP.Net using VB.Net

using System;using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;using System.Data;
using System.Net;
using System.DirectoryServices;
using System.Runtime.InteropServices;

namespace GetMacAddressFromIPAddress
{
public class GetMacAddressFromIPAddress
{
[DllImport("iphlpapi.dll", ExactSpelling=true)]
public static extern int SendARP( int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen );
public string GetMacAddress(string sName)
{
string s = string.Empty ;
System.Net.IPHostEntry Tempaddr = null;
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(sName);
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
string[] Ipaddr = new string[3];
foreach(IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
int r = SendARP( (int) TempA.Address, 0, ab, ref len );
string sMAC = BitConverter.ToString( ab, 0, 6 );
Ipaddr[2] = sMAC;
s = sMAC;
} return s;
}
}
}

No comments: