How to determine which network adapter is connected to the internet

后端 未结 6 957
死守一世寂寞
死守一世寂寞 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:52

    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);
            }
         }
    }
    

提交回复
热议问题