我正在为vb.net修改一个vpn vb6应用程序。如何使用NetworkInterface获取当前的活动连接名称和IP地址?我知道,如果vpn处于活动状态,则以太网或wifi连接不应处于活动状态,对吗?无论使用Windows vpn连接(应显示vpn名称和ip),OpenVPN(应将最可能的名称显示为以太网2)还是vpn应用(最可能的名称),我的应用程序都应列出当前的活动连接名称和ip地址。作为以太网2)。
答案 0 :(得分:1)
否,Windows会显示所有接口,并且活动接口必须至少是其中之一添加到vpn中。通过网络连接导致vpn(virtuell专用网络)隧道通过。
Dim interfaces As NetworkInformation.NetworkInterface() = NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
If interfaces.GetLongLength(0) > 0 Then
For Each Interface As NetworkInformation.NetworkInterface In interfaces
If String.Compare(Interface.Description, "Tap") > 0 And String.Compare(Interface.Description, "Teredo") < 0 Then
End if
Next
End if
这个活跃的openvpn连接。如果其他接口方法更适合识别您的VPN连接,则可以调试它并进行查找
答案 1 :(得分:0)
这是到目前为止我可以使用的代码:
Public Function GetConnectionList() As List(Of String())
Dim conList As New List(Of String())
Dim conName As String = "dummy"
Dim last_conName As String = "dummy"
Dim conIPstr As String = "dummy"
Dim str As String
conList.Clear()
Dim nic As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nic.GetLongLength(0) > 0 Then
For Each netadapter As NetworkInterface In nic
conName = netadapter.Name
Dim strArr As String() = New String(2) {"dummy", "dummy", "dummy"}
If Not conName.Contains("VirtualBox") _
And Not conName.Contains("Loopback") _
And conName <> "Ethernet" _
And conName <> "Wi-Fi" Then
strArr(0) = conName
If netadapter.OperationalStatus = OperationalStatus.Up Then
strArr(1) = "Current Sataus: Active"
Try
conIPstr = GetMyIPstr()
Catch e As Exception
MessageBox.Show("Error: Function GetConnectionList: Error returning IP Address" & vbCrLf & vbCrLf & e.Message)
End Try
strArr(2) = conIPstr
Else
strArr(1) = "Current Sataus: Not Active"
strArr(2) = "No IP Address"
End If
conList.Add(strArr)
End If
Next
Else
MessageBox.Show("Error: Function GetConnectionList: No Net Adapter List")
End If
Return conList
End Function
Private Function GetMyIPstr() As String
Dim client As New WebClient
Dim s As String = "No IP Address"
'// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)")
Dim baseurl As String = "http://checkip.dyndns.org/"
' with proxy server only:
Dim proxy As IWebProxy = WebRequest.GetSystemWebProxy()
proxy.Credentials = CredentialCache.DefaultNetworkCredentials
client.Proxy = proxy
Dim data As Stream
Try
data = client.OpenRead(baseurl)
Catch ex As Exception
MsgBox("open url " & ex.Message)
Exit Function
End Try
Dim reader As StreamReader = New StreamReader(data)
s = reader.ReadToEnd()
data.Close()
reader.Close()
s = s.Replace("<html><head><title>Current IP Check</title></head><body>", "").Replace("</body></html>", "").ToString()
'MessageBox.Show(s)
'Me.Text = s
Return s
End Function