c#获取公网IP

private void GetIP()  
{  //转载请注明来自 http://www.uzhanbao.com
    string hostName = Dns.GetHostName();//本机名    //转载请注明来自 http://www.shang11.com  
    //System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;//会警告GetHostByName()已过期,我运行时且只返回了一个IPv4的地址   
    System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//会返回所有地址,包括IPv4和IPv6   
    foreach (IPAddress ip in addressList)  
    {  
        listBox1.Items.Add(ip.ToString());  
    }  
}

 

你可能感兴趣的:(.net专题)