使用下面的方法即可获得本机的IP地址
using System.Net.Sockets;
using System.Net;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
GetLocalIP(); //获取本机IP方法
}
static void GetLocalIP()
{
string hostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostEntry(hostName);
foreach (var ipAddress in ipEntry.AddressList)
{
if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine(ipAddress.ToString());
}
}
}
}
}
运行结果:
192.168.1.60