title: Detect which network interface is in use
body: <p>I'm using the following code to get the IPv4 gateway address of a local computer</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>which is working fine, but I'm choosing the interface (the one that is active) manually. I want to choose it programmatically.</p>
  
  <p>I did some debugging. <code>NetworkInterface.GetAllNetworkInterfaces()</code> 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 <code>OperationalStatus</code>, but it's not really reliable.</p>
  
  <p>I'm connected to internet using my mobile phone (wifi hotspot) and this is the interface list I get:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>As you can see, there are two interfaces with the <code>Up</code> 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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Consequently, I can't rely on <code>OperationalStatus</code>, I think. How can I make this work?</p>