2016-09-26 64 views
4

我用尽了一切办法让我的IPv4地址...试图让我的IPv4地址获取的VirtualBox的IPv4

一些例子:

Dim s As String = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where(Function(a As IPAddress) Not a.IsIPv6LinkLocal AndAlso Not a.IsIPv6Multicast AndAlso Not a.IsIPv6SiteLocal).First().ToString() 

_

Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()) 
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString() 

_

strHostName = System.Net.Dns.GetHostName() 
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString() 

_

Dim entry = Dns.GetHostEntry(System.Net.Dns.GetHostName()) 
    For Each address In entry.AddressList 
     If address.AddressFamily = AddressFamily.InterNetwork Then 
      Return address.ToString 
     End If 
    Next 

E.t.c.

...但上述所有给我的VirtualBox的IPv4一样:

这是为什么,我该如何解决?

+1

正如你已经注意到的,你的电脑有多个IP地址。你的代码总是返回第一个(不保证顺序)。恰巧,第一个来自VirtualBox适配器。 –

+0

我可以做些什么吗? – BanForFun

+2

您可以枚举计算机中的所有网络适配器,查找符合条件(名称,网关等)的网络适配器。然后从该适配器返回地址。对不起,我现在没有可用的函数名称。 –

回答

2

好吧我没有找到一种方法来获得我想要的IP,但我找到了一种方法来获得所有可用的IPS与他们的网络适配器名称。这里是万一有人代码希望它:

首次进口System.Net.SocketsSystem.NetSystem.Net.NetworkInformation

代码:

Dim lst As New List(Of String) 
    For Each adapter As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces 
     lst.Add(adapter.Description & ": " & adapter.GetIPProperties.UnicastAddresses(1).Address.ToString) 
    Next 

lst是与所有的网络适配器及其IPS名单

+0

不错的工作!请注意,网络适配器可能有多个地址,但您的代码只会返回第一个地址。但是,这对你来说可能不是问题,如果你的代码永远不会在具有这种设置的计算机上运行。要解决它,你只需要在adapter.GetIPProperties.UnicastAddresses中做一个内部循环。 –

+0

好的,谢谢!.. :) – BanForFun