在C#中,你可以使用System.Net.NetworkInformation
命名空间中的类来获取网络适配器的状态属性,包括持续时间。以下是如何获取这些信息的步骤和示例代码:
以下是一个简单的C#示例,展示如何获取网络适配器的状态和持续时间:
using System;
using System.Net.NetworkInformation;
class Program
{
static void Main()
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
OperationalStatus status = adapter.OperationalStatus;
Console.WriteLine($"适配器名称: {adapter.Name}");
Console.WriteLine($"状态: {status}");
if (status == OperationalStatus.Up)
{
IPv4InterfaceStatistics stats = adapter.GetIPv4Statistics();
long bytesSentSpeed = stats.BytesSent / 1024; // KB/s
long bytesReceivedSpeed = stats.BytesReceived / 1024; // KB/s
Console.WriteLine($"发送速度: {bytesSentSpeed} KB/s");
Console.WriteLine($"接收速度: {bytesReceivedSpeed} KB/s");
}
Console.WriteLine();
}
}
}
通过上述方法,你可以有效地获取和管理网络适配器的状态信息。
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云