查找用户通过哪个网络设备连接到互联网

发布于 2024-10-29 03:32:08 字数 1493 浏览 3 评论 0原文

使用下面的代码,我将获得机器上启用和运行的所有网络接口。

Private netIntrfc As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For i As Integer = 0 To netIntrfc.Length - 1
  If netIntrfc(i).OperationalStatus = OperationalStatus.Up Then
    netDevicesList.Items.Add(netIntrfc(i).Name.ToString)
  End If
Next

但我的问题是如何获得默认,即用户连接到互联网的那个(以太网适配器)?

我需要更改默认适配器的一些设置(用户连接到互联网的方式)。我通过注册表更改设置,以便我可以为每个网络接口添加相同的设置,但这可能会导致问题并且毫无意义。

编辑:

现在我已经完成了下面的代码,所以如果这可以帮助其他人,但如果有人有更好的解决方案或更可靠,请发送。

Dim u As UdpClient = New UdpClient(System.Net.Dns.GetHostName, 1)
Dim localAddr As IPAddress = CType(u.Client.LocalEndPoint, IPEndPoint).Address

Private netIntrfc As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For i As Integer = 0 To netIntrfc.Length - 1
  If netIntrfc(i).OperationalStatus = OperationalStatus.Up Then
    For Each uni As NetworkInformation.UnicastIPAddressInformation In ipProps.UnicastAddresses
      If uni.Address.ToString = localAddr.ToString Then
        netDevicesList.Items.Add("DEFAULT: " & netIntrfc(i).Name.ToString)
        DEFDEVID = netIntrfc(i).Id.ToString
      End If
    Next
    netDevicesList.Items.Add(netIntrfc(i).Name.ToString)
  End If
Next

感谢Thomas-Li这篇文章

Using the code below I will get all network interfaces which are enabled and functional on the machine.

Private netIntrfc As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For i As Integer = 0 To netIntrfc.Length - 1
  If netIntrfc(i).OperationalStatus = OperationalStatus.Up Then
    netDevicesList.Items.Add(netIntrfc(i).Name.ToString)
  End If
Next

But my problem is how to get the default one, the one(ethernet adapter) through which user is connected to internet?

I need to change some settings of default(through which user is connected to internet) adapter. settings I change through registry so I could sample add same settings for each network interface but that could cause problems and makes no point then.

EDITED:

For now I have done like code below, so if this can help someone other, but if someone has a better solution or more reliable then send please.

Dim u As UdpClient = New UdpClient(System.Net.Dns.GetHostName, 1)
Dim localAddr As IPAddress = CType(u.Client.LocalEndPoint, IPEndPoint).Address

Private netIntrfc As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For i As Integer = 0 To netIntrfc.Length - 1
  If netIntrfc(i).OperationalStatus = OperationalStatus.Up Then
    For Each uni As NetworkInformation.UnicastIPAddressInformation In ipProps.UnicastAddresses
      If uni.Address.ToString = localAddr.ToString Then
        netDevicesList.Items.Add("DEFAULT: " & netIntrfc(i).Name.ToString)
        DEFDEVID = netIntrfc(i).Id.ToString
      End If
    Next
    netDevicesList.Items.Add(netIntrfc(i).Name.ToString)
  End If
Next

Thanks Thomas-Li and this post

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

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

发布评论

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

评论(2

<逆流佳人身旁 2024-11-05 03:32:08

这会给你一些提示吗?

识别活动网络接口

Will this give you some hints?

Identifying active network interface

赠佳期 2024-11-05 03:32:08

我将你的代码移植到了 c#,希望你不介意

    static void Main(string[] args)
    {
        UdpClient u = new UdpClient(System.Net.Dns.GetHostName(), 1);
        IPAddress localAddr = (u.Client.LocalEndPoint as IPEndPoint).Address;
        NetworkInterface[] netIntrfc  = NetworkInterface.GetAllNetworkInterfaces();
        for (int i = 0; i < netIntrfc.Length - 1; i++)
        {
            if (netIntrfc[i].OperationalStatus == OperationalStatus.Up) 
            {
                IPInterfaceProperties ipProps = netIntrfc[i].GetIPProperties();
                foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses) 
                {
                    if (uni.Address.ToString() == localAddr.ToString()) 
                    {
                        Console.WriteLine("DEFAULT: " + netIntrfc[i].Name.ToString());
                        Console.WriteLine(netIntrfc[i].Id.ToString());
                    }
                }
            } 
        }
    }

i ported your code to c#, i hope you don' t mind

    static void Main(string[] args)
    {
        UdpClient u = new UdpClient(System.Net.Dns.GetHostName(), 1);
        IPAddress localAddr = (u.Client.LocalEndPoint as IPEndPoint).Address;
        NetworkInterface[] netIntrfc  = NetworkInterface.GetAllNetworkInterfaces();
        for (int i = 0; i < netIntrfc.Length - 1; i++)
        {
            if (netIntrfc[i].OperationalStatus == OperationalStatus.Up) 
            {
                IPInterfaceProperties ipProps = netIntrfc[i].GetIPProperties();
                foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses) 
                {
                    if (uni.Address.ToString() == localAddr.ToString()) 
                    {
                        Console.WriteLine("DEFAULT: " + netIntrfc[i].Name.ToString());
                        Console.WriteLine(netIntrfc[i].Id.ToString());
                    }
                }
            } 
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文