我想获取计算机的本地局域网IP。我的电脑同时连接了lan和wifi。我通过使用以下程序获取lan ip,但未获取wlan ip。如何获得??
Private Function GetIPv4Address() As String
GetIPv4Address = String.Empty
Dim strHostName As String = System.Net.Dns.GetHostName()
Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)
For Each ipheal As System.Net.IPAddress In iphe.AddressList
If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
GetIPv4Address = ipheal.ToString()
End If
Next
此程序获取lan和wlan的最后连接IP的IP地址,但在任何情况下我都只希望wlan ip
下面的程序仅显示局域网
将Mem转换为字符串
Private Sub GetIPAddress1()
Dim strHostName As String
Dim strIPAddress As String
strHostName = System.Net.Dns.GetHostName()
strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()
mem = strIPAddress
MessageBox.Show("IP Address: " & strIPAddress)
End Sub
答案 0 :(得分:0)
您可以使用NetworkInformation类来访问网络接口上的所有信息。
使用NetworkInterface方法枚举NetworkInterface.GetAllNetworkInterfaces()集合时,可以添加过滤器以仅选择具有NetworkInterfaceType = NetworkInterfaceType.Wireless80211
的接口。
当/如果找到了所请求的接口,我们只需要提取IPv4 UnicastAddress,其中包含IPAddress。
Imports System.Net.NetworkInformation
Dim WirelessIPAddress As IPAddress = GetWirelessIPAddress()
'If the string form is required:
Dim WLSIPAddress As String = WirelessIPAddress.ToString()
Public Function GetWirelessIPAddress() As IPAddress
Return NetworkInterface.
GetAllNetworkInterfaces().
FirstOrDefault(Function(NI) NI.NetworkInterfaceType = NetworkInterfaceType.Wireless80211).
GetIPProperties().
UnicastAddresses.Last().
Address
End Function
请参见this method(C#
),以提取有关接口的更多信息。