C#获取本机IP

转自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 "";
}

猜你喜欢

转载自blog.csdn.net/sdhongjun/article/details/84865844