How to determine which network adapter is connected to the internet

14,723

Solution 1

I ended up following a link to MSDN when I was reading this page where I found the GetBestInterface function. I was able to use that to find the adapter thats connected to the internet

Solution 2

You can use WMI to query all the adapters and see which one is connected.
This article shows you how to do it in VB.Net (very easily transferable to C#).

Solution 3

See here for a similar question on how to monitor the bandwidth used, in VB.NET, but the philosophy is the same! Here is another question that is the fastest way of checking for internet connection.

Hope this helps, Best regards, Tom.

Solution 4

Examine the routing table and look for the interfaces that have a default route (a route to 0.0.0.0) - that's your interface(s) that are connected to the wider world (if any).

Solution 5

use below code snippet for get the current active network adapter.

using System.Net.NetworkInformation;
using System.Linq;

class Program
{
     static void Main(string[] args)
     {
        NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(o => o.OperationalStatus == OperationalStatus.Up && o.NetworkInterfaceType != NetworkInterfaceType.Tunnel && o.NetworkInterfaceType != NetworkInterfaceType.Loopback);
        if (networkInterface != null)
        {
            Console.WriteLine(networkInterface.Name);
        }
     }
}
Share:
14,723
Telanor
Author by

Telanor

Updated on July 19, 2022

Comments

  • Telanor
    Telanor almost 2 years

    I'm writing a program in C# that needs to monitor the amount of internet bandwidth currently in use so it can do a background upload when the internet usage is low. How can I automatically determine which network adapter is the one connected to the internet?

  • Joe Huang
    Joe Huang about 2 years
    You will get a virtual network interface if your machine has installed VMware or Hyper-V.