Public class netutil: system. Web. UI. Page
{
[Dllimport ("iphlpapi. dll")]
Private Static extern int sendarp (int32 DEST, int32 host, ref int64 Mac, ref int32 length );
[Dllimport ("ws2_32.dll")]
Private Static extern int32 inet_addr (string IP );
Public static string getmac (string IP)
{
If ("127.0.0.1" = IP)
{
Return getlocalhostmac ();
}
Else
{
Return getremotehostmac (IP );
}
}
Private Static string getlocalhostmac ()
{
String MAC = "";
Managementobjectsearcher query = new managementobjectsearcher ("select * From win32_networkadapterconfiguration ");
Managementobjectcollection querycollection = query. Get ();
Foreach (managementobject Mo in querycollection)
{
If (Mo ["ipenabled"]. tostring () = "true ")
MAC = Mo ["macaddress"]. tostring ();
}
MAC = Mac. Replace (":","-");
Return MAC;
}
Private Static string getremotehostmac (string IP)
{
Int64 macinfo = new int64 ();
Int32 Len = 6;
Int32 ldest = inet_addr (IP); // ip address of the destination
Int res = sendarp (ldest, 0, ref macinfo, ref Len );
String mac_src = macinfo. tostring ("X ");
While (mac_src.length <12)
{
Mac_src = mac_src.insert (0, "0 ");
}
String mac_dest = "";
for (INT I = 0; I <11; I ++)
{< br> If (0 = (I % 2 ))
{< br> if (I = 10)
{< br> mac_dest = mac_dest.insert (0, mac_src.substring (I, 2 ));
}< br> else
{< br> mac_dest = "-" + mac_dest.insert (0, mac_src.substring (I, 2 ));
}< BR >}< br> return mac_dest;
}< BR >}