From 5cf52df371d67bfb4320bd5d9d7b9c8aede4a941 Mon Sep 17 00:00:00 2001 From: Application Date: Mon, 11 Dec 2023 11:53:51 +0800 Subject: [PATCH] update NwAdapterTest (#8) updated NwAdapterTest removed SystemGatewayTest --- .../SduNetCheckTool.Core.csproj | 1 - .../Tests/NetworkAdapterTest.cs | 57 +++++++++++++------ .../Tests/SystemGatewayTest.cs | 55 ------------------ .../ViewModels/TestViewModel.cs | 1 - 4 files changed, 40 insertions(+), 74 deletions(-) delete mode 100644 SduNetCheckTool.Core/Tests/SystemGatewayTest.cs diff --git a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj index dba1c18..b5ec8af 100644 --- a/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj +++ b/SduNetCheckTool.Core/SduNetCheckTool.Core.csproj @@ -92,7 +92,6 @@ - diff --git a/SduNetCheckTool.Core/Tests/NetworkAdapterTest.cs b/SduNetCheckTool.Core/Tests/NetworkAdapterTest.cs index 089e163..1dd1835 100644 --- a/SduNetCheckTool.Core/Tests/NetworkAdapterTest.cs +++ b/SduNetCheckTool.Core/Tests/NetworkAdapterTest.cs @@ -1,6 +1,8 @@ using SduNetCheckTool.Core.Repairs; using System; using System.Collections.Generic; +using System.Linq; +using System.Net; using System.Net.NetworkInformation; namespace SduNetCheckTool.Core.Tests @@ -12,25 +14,46 @@ public Tuple Test() var retList = new List(); var hasNetConnection = false; - NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); - - foreach (NetworkInterface networkInterface in networkInterfaces) + foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { - if (networkInterface.OperationalStatus == OperationalStatus.Up) + if (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback) { - var ipProperties = networkInterface.GetIPProperties(); - var dnsAddresses = ipProperties.DnsAddresses; - - retList.Add($"网络名称: {networkInterface.Name}"); - retList.Add($"网卡描述: {networkInterface.Description}"); - retList.Add($"MAC地址: {networkInterface.GetPhysicalAddress()}"); - retList.Add($"网卡类型: {networkInterface.NetworkInterfaceType}"); - retList.Add($"网卡速度: {(networkInterface.Speed / 1000 / 1000)} Mbps"); - retList.Add($"网络连接状态: {networkInterface.OperationalStatus}"); - retList.Add($"DNS服务器地址: {string.Join(" ", dnsAddresses)}"); - retList.Add("————————————————————————————"); - if (!hasNetConnection && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback) - hasNetConnection = true; + IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties(); + + var test = networkInterface.GetIPProperties(); + + string macInfo = networkInterface.GetPhysicalAddress().ToString() != "" ? networkInterface.GetPhysicalAddress().ToString() : "--"; + string connectionSpeed = networkInterface.OperationalStatus.ToString() == "Up" ? (networkInterface.Speed / 1000 / 1000).ToString() + " Mbps" : "--"; + string dhcpServers = iPInterfaceProperties.DhcpServerAddresses.Count() > 0 ? string.Join(" ", iPInterfaceProperties.DhcpServerAddresses) : "--"; + + retList.Add($"网络名称: {networkInterface.Name}"); + retList.Add($"网卡描述: {networkInterface.Description}"); + retList.Add($"MAC地址: {macInfo}"); + // retList.Add($"网卡类型: {networkInterface.NetworkInterfaceType}"); + retList.Add($"连接状态: {networkInterface.OperationalStatus}"); + retList.Add($"连接速度: {connectionSpeed}"); + retList.Add($"DNS服务器: {string.Join(" ", iPInterfaceProperties.DnsAddresses)}"); + retList.Add($"DHCP服务器: {dhcpServers}"); + + foreach(var item in iPInterfaceProperties.UnicastAddresses) + { + retList.Add($"IP地址: {item.Address}"); + } + + foreach (var item in iPInterfaceProperties.GatewayAddresses) + { + retList.Add($"网关地址: {item.Address}"); + + Ping ping = new Ping(); + PingReply reply = ping.Send(item.Address, 200); + + string delay = reply.Status == IPStatus.Success ? reply.RoundtripTime.ToString() + " ms" : "--"; + retList.Add($"网关延迟: {delay}"); + } + + retList.Add(""); + + hasNetConnection = true; } } diff --git a/SduNetCheckTool.Core/Tests/SystemGatewayTest.cs b/SduNetCheckTool.Core/Tests/SystemGatewayTest.cs deleted file mode 100644 index a5944f2..0000000 --- a/SduNetCheckTool.Core/Tests/SystemGatewayTest.cs +++ /dev/null @@ -1,55 +0,0 @@ -using SduNetCheckTool.Core.Repairs; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Threading; - -namespace SduNetCheckTool.Core.Tests -{ - public class SystemGatewayTest : ITest - { - public Tuple Test() - { - var data = new List(); - var result = TestResult.Failed; - try - { - Ping ping = new Ping(); - - NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); - foreach (NetworkInterface networkInterface in interfaces) - { - IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties(); - GatewayIPAddressInformationCollection gatewayIPAddressInformation = iPInterfaceProperties.GatewayAddresses; - IPAddressCollection addresses = iPInterfaceProperties.DhcpServerAddresses; - foreach (var item in gatewayIPAddressInformation) - { - data.Add($"网卡信息: {networkInterface.Description}"); - data.Add($"网关地址: {item.Address}"); - PingReply reply = ping.Send(item.Address, 200); - if (reply.Status == IPStatus.Success) - { - data.Add($"网关延迟: {reply.RoundtripTime} ms"); - } - if (addresses.Count > 0) - { - foreach (IPAddress address in addresses) - { - data.Add($"DHCP服务器: {address}"); - } - } - data[data.Count - 1] = data.Last() + "\n"; - } - } - result = TestResult.Success; - } - catch (Exception) - { - //ignored - } - return new Tuple(result, string.Join("\n", data), null); - } - } -} diff --git a/SduNetCheckTool.GUI/ViewModels/TestViewModel.cs b/SduNetCheckTool.GUI/ViewModels/TestViewModel.cs index 8f30b90..3583c05 100644 --- a/SduNetCheckTool.GUI/ViewModels/TestViewModel.cs +++ b/SduNetCheckTool.GUI/ViewModels/TestViewModel.cs @@ -31,7 +31,6 @@ private void Init() new DetectionTask(new NetworkAdapterTest(),"网卡检测"), new DetectionTask(new SduNetTest(),"校园网状态检测"), new DetectionTask(new SystemProxyTest(),"系统代理检测"), - new DetectionTask(new SystemGatewayTest(),"系统网关检测"), new DetectionTask(new SduWebsiteTest(),"山大网站连通性检测"), new DetectionTask(new CommonWebsiteTest(),"常用网站检测") };