你可以通过下面的方法来获取本机的IP地址:
using System.Net; using System.Net.Sockets; public static string GetLocalIPAddress() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } return ""; }
调用这个方法即可获取本机的IP地址:
string ipAddress = GetLocalIPAddress(); Console.WriteLine(ipAddress);