Asp. NET technology to obtain IP and MAC address

Source: Internet
Author: User
Tags tostring

The method of obtaining the IP address of the server is simple and practical by DNS method, as follows:

private void Buttonip_click (object sender, System.EventArgs e)
{system.net.ipaddress[] AddressList = Dns.gethostbyname (Dns.gethostname ()). AddressList;
if (addresslist.length>1)
{textlip.text = addresslist[0]. ToString ();
Textsip.text = addresslist[1]. ToString ();
}
Else
{
Textlip.text = addresslist[0]. ToString ();
Textsip.text = "no connections available";
}
}

Another way to get the server's IP address and MAC address is as follows:

Using System.Management;
String stringmac = "";
String STRINGIP = "";
ManagementClass MC = new ManagementClass "Win32_NetworkAdapterConfiguration");
Managementobjectcollection moc= MC. GetInstances ();
foreach (ManagementObject MO in MOC)
{
if ((bool) mo["ipenabled"] = = True)
{
Stringmac + = mo["MACAddress"]. ToString ();
Textmac.text = Stringmac.tostring ();
String[] ipaddresses = (string[]) mo["IPAddress"];
if (Ipaddresses.length > 0)
STRINGIP = Ipaddresses[0];
Textip.text = Stringip.tostring ();
}
}

It is fairly easy to get the IP address of the client native, as follows:

Using System.Net;
textip.text=page.request.userhostaddress;

If you want to get the client local MAC address is relatively more complex, have to import two API, use ARP protocol to obtain, but this can only get to the same network segment of the machine Mac, for the cross section of the use of IP scanning or cmd nbtstat command to obtain the MAC address. It can also be obtained by reading the system registry value or the WMI database. Please let me know if you have any easy and workable methods.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.