How to determine which network adapter is connected to the Internet

I am writing a C # program that needs to control the amount of bandwidth currently being used on the Internet so that it can load a wallpaper when Internet usage is low. How can I automatically determine which network adapter is connected to the Internet?

+6
c # networking
source share
6 answers

In the end, I went to the MSDN link when I read this page where I found GetBestInterface . I was able to use this to find an adapter connected to the Internet

+4
source share

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

+3
source share

See here for a similar question on how to control used bandwidth in VB.NET, but the philosophy is the same! Here 's another question, which is the fastest way to check your Internet connection.

Hope this helps, Regards, Tom.

+3
source share

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

+3
source share

There are several ways that the adapter can display as “connected to the Internet” when it is not. Conversely, it may “not connect to the Internet” and still connect.

Like so many things in life, "The proof of pudding is food." If you want to know if you are connected, you need to try to say something.

I like time.gov because it returns a tiny piece of XML containing the current time, so you can make sure that you are really connecting to the network and not getting any cached data or redirecting to an involuntary portal.

Just go through the adapters and see which one really has connectivity.

0
source share

use the code snippet below to get the currently active network adapter.

 using System.Net.NetworkInformation; 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); } } } 
0
source share

All Articles