Using system.net;# region get internal, external network ip///<summary>///get local IP address, take precedence of intranet ip///</summary>public static String Getlocalip () {string[] Ips = getlocalipaddress (); foreach (String IP in Ips) if (IP. StartsWith ("10.80.")) return IP; foreach (String IP in Ips) if (IP. Contains (".")) return IP; return "127.0.0.1";} <summary>///gets the local IP address. Multiple ip///</summary>public static string[] Getlocalipaddress () {String hostName = Dns.gethostname (); Get host name ipaddress[] addresses = dns.gethostaddresses (hostName); Resolve host IP address string[] IP = new string[addresses. Length]; Convert to string form for (int i = 0; i < addresses. Length; i++) Ip[i] = Addresses[i]. ToString (); return IP;} <summary>///get the Extranet IP address///</summary>public static string Getextenalipaddress_0 () {String ip = "not acquired to extranet IP" ; try {//Get native IP data from the URL System.Net.WebClient client = new System.Net.WebClient (); Client. Encoding = System.Text.EncodIng. Default; String str = client. Downloadstring ("http://1111.ip138.com/ic.asp"); Client. Dispose (); Extract the extranet IP data [218.104.71.178] int i1 = str. IndexOf ("["), i2 = str. IndexOf ("]"); IP = str. Substring (I1 + 1, i2-1-i1); } catch (Exception) {} return IP;} <summary>///get the Extranet IP address///</summary>public static string Getextenalipaddress () {String url = "Http://hij Oyusers.joymeng.com:8100/test/getnamebyotherip "; string IP = "not acquired to extranet IP"; try {//Get native IP data from the URL System.Net.WebClient client = new System.Net.WebClient (); Client. Encoding = System.Text.Encoding.Default; String str = client. downloadstring (URL); Client. Dispose (); if (!str. Equals ("")) IP = str; else IP = Getextenalipaddress_0 (); } catch (Exception) {} return IP;} # endregion
C # Get native intranet extranet IP