First, you need to join a namespace
Using system.net.networkinformation;using System.Net.Sockets;
The following is the specific code
Getprivateip (); String publicip = Getpublicip (); Console.WriteLine ("Public IP is: {0}", PUBLICIP);//Get the common network IP address static string Getpublicip () {Strin G address = ""; WebRequest request = WebRequest.Create ("http://checkip.dyndns.org/"); using (WebResponse response = Request. GetResponse ()) using (StreamReader stream = new StreamReader (response. GetResponseStream ())) {address = stream. ReadToEnd (); }//Find the IP address from the HTML page int first = address. IndexOf ("Address:") + 9; int last = address. LastIndexOf ("</body>"); Address = address. Substring (first, Last-first); return address; }//Gets the private IP address of the static void Getprivateip () {foreach (var interfaces in Networkinterface.getallne Tworkinterfaces ()) {foreach (var address in interfaces). Getipproperties (). unicastaddresses) {if (address. address.addressfamily = = AddressFamily.InterNetwork) {Console.WriteLine ("IP ADD Ress: "+ address. Address.tostring ()); } } } }
13 How to get the IP address in C #