C# Resource Monitor get network activity values

痞子三分冷 提交于 2019-12-02 14:38:14

问题


I just want to display the values of this process on my windows app form using C#

I tried this using PerformanCounter class but I can't figure it out.

 var perfCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", "chrome");
 // Initialize to start capturing
 perfCounter.NextValue();

 for (int i = 0; i < 20; i++)
 {
     // give some time to accumulate data
     Thread.Sleep(1000);

     float receive = perfCounter.NextValue() / Environment.ProcessorCount;

     Console.WriteLine("Bytes Receive/sec: " + receive);
 }

回答1:


string pn = "MyProcessName.exe";
var readOpSec  = new PerformanceCounter("Process","IO Read Operations/sec", pn);
var writeOpSec = new PerformanceCounter("Process","IO Write Operations/sec", pn);
var dataOpSec  = new PerformanceCounter("Process","IO Data Operations/sec", pn);
var readBytesSec = new PerformanceCounter("Process","IO Read Bytes/sec", pn);
var writeByteSec = new PerformanceCounter("Process","IO Write Bytes/sec", pn);
var dataBytesSec = new PerformanceCounter("Process","IO Data Bytes/sec", pn);

var counters = new List<PerformanceCounter>
                {
                readOpSec,
                writeOpSec,
                dataOpSec,
                readBytesSec,
                writeByteSec,
                dataBytesSec
                };

// get current value
foreach (PerformanceCounter counter in counters)
{
    float rawValue = counter.NextValue();

    // display the value
}

Try this question : retrieve-process-network-usage



来源:https://stackoverflow.com/questions/48397626/c-sharp-resource-monitor-get-network-activity-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!