.Net IPAddress IPv4

前端 未结 3 2043
悲&欢浪女
悲&欢浪女 2021-02-06 05:52

I have the following code:

Dim ipAdd As IPAddress = Dns.GetHostEntry(strHostname).AddressList(0)
Dim strIP As String = ipAdd.ToString()

When I

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 06:48

    Instead of unconditionally taking the first element of the AddressList, you could take the first IPv4 address:

    var address = Dns.GetHostEntry(strHostname)
                     .AddressList
                     .First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
    

提交回复
热议问题