c# 网速监控
2021-04-05 21:29
标签:csv 接收 本地计算机 mon col == -- timer turn c# 网速监控 标签:csv 接收 本地计算机 mon col == -- timer turn 原文地址:https://www.cnblogs.com/wurong/p/9149829.html class Program
{
//实例化一个性能计数器,统计网卡流量:发送,字节
private static System.Diagnostics.PerformanceCounter _sendCounter;
//实例化一个性能计数器,统计网卡流量:接收,字节
private static System.Diagnostics.PerformanceCounter _receivedCounter;
//打印的地址
private static readonly string filePath = System.IO.Directory.GetCurrentDirectory() + "\\NetworkMonitor.csv";
static void Main(string[] args)
{
Console.Title = "流量监控";
var adapter = GetNetworkInterface();
Console.WriteLine("当前连接名称: "+ adapter.Name);
if (adapter.NetworkInterfaceType.ToString() == "Loopback")
{
Console.WriteLine("当前暂无网络连接");
Console.ReadLine();
return;
}
else
{
//实例化一个性能计数器,统计网卡流量:发送,字节
_sendCounter = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", adapter.Description);
//实例化一个性能计数器,统计网卡流量:接收,字节
_receivedCounter = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Received/sec", adapter.Description);
WriteMessage(filePath, "当前时间,上行,下行");
var time = ConfigurationManager.AppSettings["Timer"];
System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(Counter), null,
0, Convert.ToInt32(time));
}
Console.ReadLine();
}
//定时输出
private static void Counter(object state)
{
float sed = _sendCounter.NextValue() / 1024;
float red = _receivedCounter.NextValue() / 1024;
StringBuilder str = new StringBuilder();
str.Append("上行: " + sed.ToString("0.00") + "kbps\r\n");
str.Append("下行: " + red.ToString("0.00") + "kbps\r\n");
str.Append("---------------------------------------\r\n");
Console.WriteLine(str);
WriteMessage(filePath, DateTime.Now + "," + sed.ToString("0.00") + "," + red.ToString("0.00"));
}
///
上一篇:【C#】解析析构函数