C # How to obtain the local IPv4 address in win7 or win8

Source: Internet
Author: User

Some time ago I wrote a blog post titled C # differences between obtaining local IP addresses in xp, win7, and win8, later, we found that the above method is incorrect for the win8 system when the network is disconnected, and the system will prompt that the access is out of bounds.

Now, the latest code is posted. This Code is applicable to xp, win7, and win8. As we all know, both Windows 8 and Windows 8 support IPv6, so we cannot use the previous method.

Static void Main (string [] args) {string HostName = Dns. getHostName (); IPHostEntry IpEntry = Dns. getHostEntry (HostName); IPAddress ip = null; for (int I = 0; I <IpEntry. addressList. length; I ++) {if (IpEntry. addressList [I]. addressFamily. toString () = "InterNetwork") {ip = IpEntry. addressList [I]; break;} Console. writeLine (ip );}

The above Code ensures that the IPv4 address is obtained.

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.