string name = Dns.GetHostName(); IPAddress[] ips = Dns.GetHostAddresses(name); foreach (IPAddress ip in ips) Console.WriteLine(ip);如果是ADSL拨号,一般ips[1]是外网地址 如果是路由器上网,没有外网地址的
static void Main(string[] args) { string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); WebRequest wr = WebRequest.Create(uri); Stream s = wr.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.Default); string all = sr.ReadToEnd(); //读取网站的数据 int start= all.IndexOf("[") + 1; int end=all.IndexOf("]",start); string tempip = all.Substring(start, end - start); Console.WriteLine(tempip); sr.Close(); s.Close(); Console.ReadLine(); }
System.Net.IPHostEntry localhost;
System.Net.IPAddress []localaddr; hostname = System.Net.Dns.GetHostName();
localhost = System.Net.Dns.GetHostEntry(hostname);
localaddr = localhost.AddressList; //localaddr中就是本机ip地址
IPAddress[] ips = Dns.GetHostAddresses(name);
foreach (IPAddress ip in ips)
Console.WriteLine(ip);如果是ADSL拨号,一般ips[1]是外网地址
如果是路由器上网,没有外网地址的
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据 int start= all.IndexOf("[") + 1;
int end=all.IndexOf("]",start);
string tempip = all.Substring(start, end - start);
Console.WriteLine(tempip); sr.Close();
s.Close();
Console.ReadLine();
}