简体   繁体   中英

Detect which network interface is in use

I'm using the following code to get the IPv4 gateway address of a local computer

var interfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
var wifi = interfaces[3];
var gatewayAddresses = wifi.GetIPProperties().GatewayAddresses;
var gateway = gatewayAddresses[0].Address.ToString();
IPv4GatewayValueLabel.Text = gateway;

which is working fine, but I'm choosing the interface (the one that is active) manually. I want to choose it programmatically.

I did some debugging. NetworkInterface.GetAllNetworkInterfaces() returns a list of interfaces. In my case, there are 7. I looked for a property that distinguishes the active interface from the other ones, and found OperationalStatus , but it's not really reliable.

I'm connected to internet using my mobile phone (wifi hotspot) and this is the interface list I get:

0:
    Description         "Microsoft Wi-Fi Direct Virtual Adapter"
    OperationalStatus   Down
1:
    Description         "Bluetooth Device (PAN)"
    OperationalStatus   Down
2:
    Description         "Realtek PCIe FE Family Controller"
    OperationalStatus   Down
3:
    Description         "Dell Wireless 1701 802.11b/g/n"
    OperationalStatus   Up
4:
    Description         "Software Loopback Interface 1"
    OperationalStatus   Up
5:
    Description         "Microsoft ISATAP Adapter"
    OperationalStatus   Down
6:
    Description         "Teredo Tunneling Pseudo-Interface"
    OperationalStatus   Down

As you can see, there are two interfaces with the Up value. Besides I wasn't sure if this list was fixed, so this time I connected using usb tethering and the interface list changed a little:

0:
    Description         "SAMSUNG Mobile USB Remote NDIS Network Device"
    OperationalStatus   Up
1:
    Description         "Microsoft Wi-Fi Direct Virtual Adapter"
    OperationalStatus   Down
2:
    Description         "Bluetooth Device (PAN)"
    OperationalStatus   Down
3:
    Description         "Realtek PCIe FE Family Controller"
    OperationalStatus   Down
4:
    Description         "Dell Wireless 1701 802.11b/g/n"
    OperationalStatus   Down
5:
    Description         "Software Loopback Interface 1"
    OperationalStatus   Up
6:
    Description         "Teredo Tunneling Pseudo-Interface"
    OperationalStatus   Up
7:
    Description         "Microsoft ISATAP Adapter #2"
    OperationalStatus   Down

Consequently, I can't rely on OperationalStatus , I think. How can I make this work?

You are on the right lines and probably have already figured this out but this is what I use and so far it has worked. Only problem i could think of is if someone was using a different vm software other then Hyper-V as I am specifically looking for that.

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in nics)
{
  if (nic.NetworkInterfaceType != NetworkInterfaceType.Loopback 
      && nic.NetworkInterfaceType != NetworkInterfaceType.Tunnel
      && nic.OperationalStatus == OperationalStatus.Up 
      && nic.Name.StartsWith("vEthernet") == false 
      && nic.Description.Contains("Hyper-v") == false)
  {
    //Do something
    break;
  }
}

perhaps On a physical system (cant comment about virtual client), you can also check the NetworkInterfaceType ( NetworkInterfaceType.Wireless80211 & NetworkInterfaceType.Ethernet ) along with OperationalStatus to check which is a real physical hardware adaptor.

However, I have never tried the configuration where wifi radio is on & the system is also connected to a ethernet cable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM