轉載:
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(); }