C# get system network usage

前端 未结 3 1566
予麋鹿
予麋鹿 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:54

    You can get the current system's network usage by working with PerformanceCounterCategory too:

    using System;
    using System.Diagnostics;
    
    class Program
    {
        static void Main()
        {
            PerformanceCounterCategory pcg = new PerformanceCounterCategory("Network Interface");
            string instance = pcg.GetInstanceNames()[0];
            PerformanceCounter pcsent = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance);
            PerformanceCounter pcreceived = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance);
    
            Console.WriteLine("Bytes Sent: {0}", pcsent.NextValue() / 1024);
            Console.WriteLine("Bytes Received: {0}", pcreceived.NextValue() / 1024);
        }
    }
    

    Source: http://dotnet-snippets.com/snippet/show-network-traffic-sent-and-received/580

提交回复
热议问题