From 626192c5ea187d372a887aec8e8f752e8e95a9df Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Tue, 22 Sep 2020 22:42:15 +0800 Subject: [PATCH] detach find ManagementObject method,Fix ICS checkbox not displaying status correctly --- Netch/Controllers/ICSController.cs | 31 ++++++++++++++---------------- Netch/Forms/SettingForm.cs | 3 ++- Netch/Utils/WMI.cs | 20 +++++++++++++++++++ 3 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 Netch/Utils/WMI.cs diff --git a/Netch/Controllers/ICSController.cs b/Netch/Controllers/ICSController.cs index 16d3aa4a..0e668579 100644 --- a/Netch/Controllers/ICSController.cs +++ b/Netch/Controllers/ICSController.cs @@ -51,26 +51,23 @@ namespace Netch.Controllers ushort[] gatewayMetric = null; string[] dns = null; - ManagementObject outboundWmi = null; - foreach (ManagementObject mo in moc) + var outboundWmi = WMI.GetManagementObjectByDeviceNameOrDefault(Global.Outbound.Adapter.Description); + + if (outboundWmi == null) { - if (((string) mo["Caption"]).EndsWith(Global.Outbound.Adapter.Description)) - { - outboundWmi = mo; - if (!(dhcpEnabled = (bool) mo["DHCPEnabled"])) - { - ipAddress = (string[]) mo["IPAddress"]; - subnetMask = (string[]) mo["IPSubnet"]; - gateway = (string[]) mo["DefaultIPGateway"]; - gatewayMetric = (ushort[]) mo["GatewayCostMetric"]; - dns = (string[]) mo["DNSServerSearchOrder"]; + return false; + } - ipAddress = new[] {ipAddress[0]}; - subnetMask = new[] {subnetMask[0]}; - } + if (!(dhcpEnabled = (bool) outboundWmi["DHCPEnabled"])) + { + ipAddress = (string[]) outboundWmi["IPAddress"]; + subnetMask = (string[]) outboundWmi["IPSubnet"]; + gateway = (string[]) outboundWmi["DefaultIPGateway"]; + gatewayMetric = (ushort[]) outboundWmi["GatewayCostMetric"]; + dns = (string[]) outboundWmi["DNSServerSearchOrder"]; - break; - } + ipAddress = new[] {ipAddress[0]}; + subnetMask = new[] {subnetMask[0]}; } #endregion diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index d92ce54c..5a44a94d 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -415,7 +415,8 @@ namespace Netch.Forms { if (ICSCheckBox.Checked) { - ICSCheckBox.Checked = ICSController.Enable(); + if (!ICSController.Enabled) + ICSCheckBox.Checked = ICSController.Enable(); } else { diff --git a/Netch/Utils/WMI.cs b/Netch/Utils/WMI.cs new file mode 100644 index 00000000..37def91a --- /dev/null +++ b/Netch/Utils/WMI.cs @@ -0,0 +1,20 @@ +using System.Management; + +namespace Netch.Utils +{ + static internal class WMI + { + public static ManagementObject GetManagementObjectByDeviceNameOrDefault(string deviceName) + { + foreach (ManagementObject mo in new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances()) + { + if (((string) mo["Caption"]).EndsWith(deviceName)) + { + return mo; + } + } + + return null; + } + } +} \ No newline at end of file