C#------如何获取本机IP地址
转载:
http://www.narkii.com/club/thread-396944-1.html
代码:
添加引用
using System.Net.Sockets; using System.Net;
private string GetIpAddress() { string hostName = Dns.GetHostName(); //获取本机名 IPHostEntry localhost = Dns.GetHostByName(hostName); //方法已过期,可以获取IPv4的地址 //IPHostEntry localhost = Dns.GetHostEntry(hostName); //获取IPv6地址 IPAddress localaddr = localhost.AddressList[0]; return localaddr.ToString(); }