Get local IP address

前端 未结 26 3008
野的像风
野的像风 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 11:03

    Other way to get IP using linq expression:

    public static List GetAllLocalIPv4(NetworkInterfaceType type)
    {
        return NetworkInterface.GetAllNetworkInterfaces()
                       .Where(x => x.NetworkInterfaceType == type && x.OperationalStatus == OperationalStatus.Up)
                       .SelectMany(x => x.GetIPProperties().UnicastAddresses)
                       .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
                       .Select(x => x.Address.ToString())
                       .ToList();
    }
    

提交回复
热议问题