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.