C# 获取外网内网IP地址

来源:百度文库 编辑:神马文学网 时间:2024/04/30 03:11:33
http://topic.csdn.net/u/20090610/21/29d1d713-1876-4369-b40a-924d41653ebb.html 引用:using System.Net.根据计算机名 获取ip地址,下面是代码: 
string hostName=Dns.GetHostName();
 this.txtName.Text=hostName;
 IPHostEntry Entry=Dns.GetHostByName(Dns.GetHostName());
 IPAddress pAddress=new IPAddress(Entry.AddressList[0].Address);   //内网地址 IPAddress wAddress=new IPAddress(Entry.AddressList[1].Address);   //外网地址 但是对于使用自动获取IP的本机,则无法取得外网的IP      2010-11-16
 this.txtIDAddress.Text=pAddress.ToString();
  以下代码不知道是否正确可用?2010-11-16             //获取外网IP地址
            System.Net.WebClient client = new System.Net.WebClient();
            client.Encoding = System.Text.Encoding.Default;
            string reply = client.DownloadString("http://www.ip138.com/ip2city.asp");//此地址不可随意改变?
            MessageBox.Show(reply);