From 75bf753a658a3644064a8f5e48b9b274d1ca488c Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 8 Oct 2020 20:30:42 +0800 Subject: [PATCH] reafactor: ICS Check State --- Netch/Forms/SettingForm.cs | 7 ++++--- Netch/Utils/ICSHelper.cs | 17 ++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 1bcb0914..9a331613 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -39,10 +39,11 @@ namespace Netch.Forms ProxyDNSCheckBox.Checked = Global.Settings.TUNTAP.ProxyDNS; UseFakeDNSCheckBox.Checked = Global.Settings.TUNTAP.UseFakeDNS; - if (TUNTAPController.SearchTapAdapter()) + var icsHelperEnabled = ICSHelper.Enabled; + if (icsHelperEnabled != null) { ICSCheckBox.Enabled = true; - ICSCheckBox.Checked = ICSHelper.Enabled; + ICSCheckBox.Checked = (bool) icsHelperEnabled; } // Behavior @@ -392,7 +393,7 @@ namespace Netch.Forms { if (ICSCheckBox.Checked) { - if (!ICSHelper.Enabled) + if (!(ICSHelper.Enabled ?? true)) ICSCheckBox.Checked = ICSHelper.Enable(); } else diff --git a/Netch/Utils/ICSHelper.cs b/Netch/Utils/ICSHelper.cs index 3f9385b3..7306b223 100644 --- a/Netch/Utils/ICSHelper.cs +++ b/Netch/Utils/ICSHelper.cs @@ -9,16 +9,19 @@ namespace Netch.Utils { public static class ICSHelper { - public static bool Enabled + public static bool? Enabled { get { - TUNTAPController.SearchTapAdapter(); - return ( - from NetworkConnection connection in new NetworkConnectionCollection() - where connection.DeviceName == Global.TUNTAP.Adapter.Description - select connection.SharingEnabled - ).FirstOrDefault(); + if (Global.TUNTAP.Adapter == null) + TUNTAPController.SearchTapAdapter(); + + foreach (var connection in new NetworkConnectionCollection().Cast().Where(connection => connection.DeviceName == Global.TUNTAP.Adapter?.Description)) + { + return connection.SharingEnabled; + } + + return null; } }