如何获取上次使用的网络连接的名称?

发布于 2024-12-24 03:51:12 字数 127 浏览 1 评论 0原文

我有一个设备(USB调制解调器),可以在计算机中安装很多网络连接,用户可以随时更改网络连接,也可以随时断开连接。然后我需要获取最后使用的网络连接,这可能吗? windows把它保存在什么东西里?

欢迎采用不同的方法来解决它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-12-31 03:51:12

我认为您无法获得“最后一个连接”,但您可以获得活动连接,并且还可以通过网络命名空间中的事件检测“网络断开/连接”。

这是我之前使用过的一些 VB.Net 代码,但您应该明白其意图。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文