using System;
using System.Net.NetworkInformation;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
ShowNetworkInterfaces();
Console.Read();
}
public static void ShowNetworkInterfaces()
{
IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Interface information for {0}.{1} ",
computerProperties.HostName, computerProperties.DomainName);
if (nics == null || nics.Length < 1)
{
Console.WriteLine(" No network interfaces found.");
return;
}
foreach (NetworkInterface adapter in nics)
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
while (true)
{
var start = adapter.GetIPv4Statistics().BytesReceived;
System.Threading.Thread.Sleep(1000);
var end = adapter.GetIPv4Statistics().BytesReceived;
var res = (end - start);
if (res > 0)
{
Console.WriteLine($"{adapter.Name}:{(res/1024)} kb,speed:{adapter.Speed / (1024 * 1024)}MB");
}
}
});
}
}
}
}
基于.net core 3.1
参考说明:NetworkInterface Class (System.Net.NetworkInformation) | Microsoft Docs