C# get system network usage

前端 未结 3 1530
予麋鹿
予麋鹿 2021-01-31 06:18

I need a way to get the current system network usage, up and down.

I found some on the net but they\'re not working out for me.

Thanks for your help

Code

3条回答
  •  礼貌的吻别
    2021-01-31 06:55

    Please see Report information from NetworkInterface: network statistics for a good sample program:

    using System;
    using System.Net.NetworkInformation;
    
    class MainClass
    {
        static void Main()
        {
            if (!NetworkInterface.GetIsNetworkAvailable())
               return;
    
            NetworkInterface[] interfaces 
                = NetworkInterface.GetAllNetworkInterfaces();
    
            foreach (NetworkInterface ni in interfaces)
            {                
                Console.WriteLine("    Bytes Sent: {0}", 
                    ni.GetIPv4Statistics().BytesSent);
                Console.WriteLine("    Bytes Received: {0}",
                    ni.GetIPv4Statistics().BytesReceived);
            }
        }
    }
    

提交回复
热议问题