C# 查找用户通过哪个网络设备连接到internet

C# 查找用户通过哪个网络设备连接到internet,c#,vb.net,visual-studio,networking,network-programming,C#,Vb.net,Visual Studio,Networking,Network Programming,使用下面的代码,我将获得机器上已启用且功能正常的所有网络接口 Private netIntrfc As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces() For i As Integer = 0 To netIntrfc.Length - 1 If netIntrfc(i).OperationalStatus = OperationalStatus.Up Then netDevicesList.Items.

使用下面的代码,我将获得机器上已启用且功能正常的所有网络接口

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
但我的问题是如何获得默认值,即用户通过它连接到internet的那一个(以太网适配器)

我需要更改一些默认设置(用户通过它连接到internet)适配器。我通过注册表更改设置,这样我就可以为每个网络接口添加相同的设置,但这可能会导致问题,也没有意义

编辑:

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

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

谢谢,这会给你一些提示吗


这会给你一些提示吗

我把你的代码移植到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());
                    }
                }
            } 
        }
    }
static void Main(字符串[]args)
{
UdpClient u=新的UdpClient(System.Net.Dns.GetHostName(),1);
IPAddress localAddr=(u.Client.LocalEndPoint作为IPEndPoint);
NetworkInterface[]netIntrfc=NetworkInterface.GetAllNetworkInterfaces();
对于(int i=0;i
我把你的代码移植到了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());
                    }
                }
            } 
        }
    }
static void Main(字符串[]args)
{
UdpClient u=新的UdpClient(System.Net.Dns.GetHostName(),1);
IPAddress localAddr=(u.Client.LocalEndPoint作为IPEndPoint);
NetworkInterface[]netIntrfc=NetworkInterface.GetAllNetworkInterfaces();
对于(int i=0;i
这会给你一些提示吗?是的,这对我很有帮助:)希望它能正常工作。我没有太多的资源来测试这一点,但对于我来说,在我的电脑上,有3个以太网卡和一个无线网卡,它能正常工作:)谢谢。你可以作为答案发布,所以我接受你;)这能给你一些提示吗?是的,这对我很有帮助:)希望它能正常工作。我没有太多的资源来测试这一点,但对于我来说,在我的电脑上,有3个以太网卡和一个无线网卡,它能正常工作:)谢谢。你可以作为答案发布,所以我接受你;)