C # obtain the local IP address and MAC address

Source: Internet
Author: User

I found several methods. After debugging and modification, the following method can be used to obtain the local IP address and MAC address. It can be used to implement this function. It is mainly used to add a reference to system. Management.

Using system; using system. management; using system. net; public class program {static void main (string [] ARGs) {try {string IP = ""; string MAC = ""; managementclass MC; string hostinfo = DNS. gethostname (); // ip address // system. net. IPaddress [] Addresslist = DNS. gethostbyname (DNS. gethostname ()). addresslist; this obsolete system. net. IPaddress [] Addresslist = DNS. gethostentry (DNS. gethostname ()). addresslist; For (INT I = 0; I <Addresslist. length; I ++) {IP = Addresslist [I]. tostring ();} // MAC address MC = new managementclass ("win32_networkadapterconfiguration"); managementobjectcollection MOC = MC. getinstances (); foreach (managementobject Mo in MoC) {If (Mo ["ipenabled"]. tostring () = "true") {MAC = Mo ["macaddress"]. tostring () ;}// output string outputstr = "IP: {0}, \ n MAC address: {1}"; outputstr = string. format (outputstr, IP, Mac); console. writeline (outputstr);} catch (exception e) {} console. readline ();}}

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.