ASP. NET get IP and Mac [using C #]

Source: Internet
Author: User

Obtain the server'sIPAddress methodDNSThe method is simple and practical, as follows:

Using system. net;

 

 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 available connections ";

}

}

Obtain the server'sIPAddress andMacAnother address method 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 ();

}

}

 

Obtain the localIPThe address is quite simple. The method is as follows:

Using system. net;

 

Textip. Text = page. Request. userhostaddress;

To obtain the local Mac The address is more complex. You have to import and call two API , Use ARP Obtain the protocol, but only Mac For the profit of cross-network segments IP Scan or CMD Medium NBTSTAT Command to obtain Mac Address. You can also read the system registry value or WMI . If you have a simple and feasible solution, please leave a message to me.

 

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.