.net 多个NIC时的用户IP地址
我想获取用户的IP地址(登录用户将在本地PC上的用户上下文下运行应用程序),但我们环境中的许多PC都有多个由VMWare Workstation添加的NIC,我想排除这些类型的桥接连接,只在PC上显示“主”NIC 以下函数将获取IPv4地址,但在我的测试PC上,它返回的是桥接连接,而不是面向网络的NIC的IP地址.net 多个NIC时的用户IP地址,.net,vb.net,.net,Vb.net,我想获取用户的IP地址(登录用户将在本地PC上的用户上下文下运行应用程序),但我们环境中的许多PC都有多个由VMWare Workstation添加的NIC,我想排除这些类型的桥接连接,只在PC上显示“主”NIC 以下函数将获取IPv4地址,但在我的测试PC上,它返回的是桥接连接,而不是面向网络的NIC的IP地址 Shared Function GetIP(ByVal computerName As String) As String 'Dim ipEntry As IPHostEnt
Shared Function GetIP(ByVal computerName As String) As String
'Dim ipEntry As IPHostEntry = Dns.GetHostEntry(computerName)
'Dim tmpAddr As IPAddress() = ipEntry.AddressList
Dim ipAddress As String = ""
'Dim i As Integer = 0
'Do While i < tmpAddr.Length
' If tmpAddr(i).AddressFamily = Sockets.AddressFamily.InterNetwork Then
' ipAddress = tmpAddr(i).ToString
' End If
' i += 1
'Loop
Dim ipentry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("")
For i As Integer = 0 To ipentry.AddressList.Count - 1
ipAddress = System.Net.Dns.GetHostEntry("").AddressList(i).ToString
Next
Return ipAddress
End Function
共享函数GetIP(ByVal computerName作为字符串)作为字符串
'Dim ipEntry As IPHostEntry=Dns.GetHostEntry(computerName)
'Dim tmpAddr As IPAddress()=ipEntry.AddressList
Dim ipAddress As String=“”
'Dim i作为整数=0
“当我
我的用户混合使用DHCP和静态地址,因此无法将NIC限制为这两种连接类型之一。我们倾向于使用172.16.x.x IP范围,那么是否有办法修改上述函数,使其只返回172.16.x.x地址
非常感谢你的帮助
谢谢
马特您可以随时使用
If ipAddress.ToString().StartsWith("172.16) Then
' Now you've got a match
ipAddress = tmpAddr(i).ToString()
End If
当然,我的代码很糟糕,因为您要调用.ToString()两次,如果有很多地址需要解析,这可能会对性能造成影响,因此您可能需要稍微修改一下…这可能会有所帮助,或者提供有关其他方法的提示
'Imports System.Net
'get all IP addresses on PC
Dim IPs As System.Net.IPHostEntry = Dns.GetHostEntry("")
'look for IPv4 that starts with 172.16
For Each IPaddr As System.Net.IPAddress In IPs.AddressList
'check for IPv4
If IPaddr.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
If Not System.Net.IPAddress.IsLoopback(IPaddr) Then 'not loopback
If IPaddr.ToString.StartsWith("172.16") Then
'found it, see if matching gateway
Dim adapters() As NetworkInformation.NetworkInterface
adapters = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
For Each adapter As NetworkInformation.NetworkInterface In adapters
Dim adapterProperties As NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
Dim addresses As NetworkInformation.GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses
If addresses.Count > 0 Then
Dim addr As NetworkInformation.GatewayIPAddressInformation
For Each addr In addresses
If addr.Address.ToString.StartsWith("172.16") Then
Stop
End If
Next addr
End If
Next adapter
End If
End If
End If
Next
工作非常完美,我将分配更改为tmpAddr,并检查tmpAddr是否以172.16开始,然后检查tmpAddr是否为true将tmpAddr分配给ipAddress。谢谢你的快速帮助。为什么对一个5岁的问题投反对票?