Get local IP address

前端 未结 26 3000
野的像风
野的像风 2020-11-22 10:07

In the internet there are several places that show you how to get an IP address. And a lot of them look like this example:

String strHostName = string.Empty;         


        
26条回答
  •  鱼传尺愫
    2020-11-22 10:51

    Updating Mrchief's answer with Linq, we will have:

    public static IPAddress GetLocalIPAddress()
    {
       var host = Dns.GetHostEntry(Dns.GetHostName());
       var ipAddress= host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
       return ipAddress;
    }
    

提交回复
热议问题