转自https://stackoverflow.com/questions/37977619/get-local-ip-address-of-ethernet-interface-in-c-sharp
using System.Net;
using System.Net.Sockets;
public static string GetIpV4()
{
try
{
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
{
socket.Connect("8.8.8.8", 1337);
IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
return endPoint.Address.ToString();
}
}
catch (Exception)
{
}
return "";
}
本文介绍了一种使用C#编程语言从以太网接口获取本地IPv4地址的方法。通过创建一个Socket并连接到外部IP,可以有效地获取本地计算机的IP地址。
175

被折叠的 条评论
为什么被折叠?



