Reputation: 16724
I have a device (USB modem) that can install in the computer a lot of network connection, the user can change the network connection any moment and can be disconnected any moment. then I need get the last network connection used, it's possible? the windows save it in any thing?
Different ways to solve it are welcomed.
Upvotes: 0
Views: 509
Reputation: 4768
I don't think you can get the "last connection", but you can get the Active Connection and you can also detect "Network Disconnected/Connected" via events from the Networking namespace.
This is some VB.Net code that I've used previously, but you should see the intent.
Dim networkAvailable As Boolean = Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()
AddHandler System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged, AddressOf NetworkAddressChanged
AddHandler System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged, AddressOf NetworkAvailabilityChanged
Private Sub OutputNetworkAddresses(message As String, linePrefix As String)
Dim adapters() As Net.NetworkInformation.NetworkInterface
adapters = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
For Each adapter As Net.NetworkInformation.NetworkInterface In adapters
Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
For Each ipi As IPAddressInformation In properties.UnicastAddresses
Dim ip As Net.IPAddress = ipi.Address
If ip.IsIPv6LinkLocal Or ip.Equals(IPAddress.Any) Or ip.Equals(IPAddress.IPv6Any) Or IPAddress.IsLoopback(ip) Then
'Debug.Print("IPv6=" & ip.ToString)
Else
Console.WriteLine(String.Format("{0} - {1}", linePrefix, ip.ToString()))
End If
Next
Next
End Sub
Upvotes: 0