How to determine which network adapter is connected to the internet

后端 未结 6 952
死守一世寂寞
死守一世寂寞 2020-12-20 20:23

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 ca

6条回答
  •  甜味超标
    2020-12-20 20:59

    There are any number of ways an adapter may show as "connected to the internet" when it isn't. Conversely, it's possible for it to "not be connected to the internet" and still be connected.

    Like many things in life, "The proof of the pudding is in the eating" If you want to know if you're connected, you'll need to try to talk to something.

    I like time.gov since it returns a tiny chunk of XML containing the current time so you can make sure you're actually connecting to the net and not getting some sort of cached data or a redirect to a captive portal.

    Just loop through the adapters and see which actually has connectivity.

提交回复
热议问题