2011-04-01 54 views
1

使用下面的代码我将获得所有在机器上启用和运行的网络接口。查找通过哪个网络设备用户连接到互联网

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 

但我的问题是如何让默认之一,通过至极用户一个(以太网适配器)连接到互联网?

我需要更改默认设置(,通过最终用户连接到互联网)适配器。设置我通过注册表更改,所以我可以为每个网络接口添加相同的设置,但这可能会导致问题,并使没有意思,然后:D

任何人都可以帮忙吗?谢谢! ;)

编辑:

现在我已经做过类似下面的代码,因此,如果这可以帮助其他人... :) 但如果有人有连击的解决方案或更可靠然后发送,请

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-Lithis post

+1

这会不会给你一些提示吗? http://stackoverflow.com/questions/359596/identifying-active-network-interface-in-net – 2011-04-01 22:38:03

+0

是的,帮助我:)希望它会工作好吧我没有太多的资源来测试这一点,但至于我在我的电脑与3个以太网卡和一个无线它工作正常:)谢谢。你可以发表答复,所以我接受你;) – FeRtoll 2011-04-01 23:02:04

回答

-1

我移植代码到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()); 
        } 
       } 
      } 
     } 
    } 
相关问题