PerformanceCounter基础知识:
参考
利用PerformanceCounter进行网卡流量监控
方法一:利用PerformanceCounter实例.NextValue()计算网卡下载速率
NextValue()使用注意(文章末尾)
namespace GPS222
{
public partial class Form2 : Form
{
private PerformanceCounter mCounter;//当前计数器
private void begin_n_Click(object sender, EventArgs e)
{
timer2_net.Interval = 1000;//。1秒一次timer2_Tick事件
timer2_net.Enabled = true;
//初始化Counter
PerformanceCounterCategory pcCategory = new PerformanceCounterCategory("Network Interface");
string[] iNames = pcCategory.GetInstanceNames();//此函数第一次执行特别耗时(不知道为什么)
PerformanceCounter[] pCounters = pcCategory.GetCounters(iNames[1]);//iNames[0]为"ASIX AX88772C USB2.0 to Fast Ethernet Adapter";iNames[1]为"Intel[R] Ethernet Connection [7] I219-V"
//给网络监控计数器赋值
mCounter = pCounters[0];
mCounter.NextValue();//初始值
}
private void timer2_net_Tick(object sender, EventArgs e)
{
double SpeedKbps = mCounter.NextValue() * 8 / 1000;
if ((SpeedKbps / 1000) > 1)
{
SpeedSendToolLabel.Text = String.Format("{0:f1} Mbps", SpeedKbps / 1000); //得到该适配器的上传速度
}
else
{
SpeedSendToolLabel.Text = String.Format("{0:f1} Kbps", SpeedKbps); //得到该适配器的上传速度
}
}
方法二(繁琐):自定义了两个类:MyNetWorkMatchClass和MyNetWorkMonitor,利用PerformanceCounter实例.NextSample().RawValue自行计算网卡上传下载速率(CaculateAndRefresh()函数)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Timers;
using System.Diagnostics;//Stopwatch\PerformanceC