From d73701340eb50e72b4114d885444ced87e430cf2 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 14 Aug 2021 23:07:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8DNetsh=EF=BC=8C=E5=9B=A0?= =?UTF-8?q?=E4=B8=BAWMI=E5=BC=95=E8=B5=B7=E5=8F=91=E5=B8=83=E6=97=B6?= =?UTF-8?q?=E8=87=B4=E5=91=BD=E9=94=99=E8=AF=AF(https://github.com/dotnet/?= =?UTF-8?q?runtime/issues/57406)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/FastGithub.Dns.csproj | 1 - FastGithub.Dns/SystemDnsUtil.cs | 31 +++++++++++++++------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/FastGithub.Dns/FastGithub.Dns.csproj b/FastGithub.Dns/FastGithub.Dns.csproj index 2300b43..ea1d4dc 100644 --- a/FastGithub.Dns/FastGithub.Dns.csproj +++ b/FastGithub.Dns/FastGithub.Dns.csproj @@ -7,7 +7,6 @@ - diff --git a/FastGithub.Dns/SystemDnsUtil.cs b/FastGithub.Dns/SystemDnsUtil.cs index 3595100..67a5cc2 100644 --- a/FastGithub.Dns/SystemDnsUtil.cs +++ b/FastGithub.Dns/SystemDnsUtil.cs @@ -1,8 +1,8 @@ using FastGithub.Configuration; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; -using System.Management; using System.Net; using System.Net.NetworkInformation; using System.Runtime.InteropServices; @@ -94,23 +94,26 @@ namespace FastGithub.Dns /// /// /// - /// - private static bool SetNameServers(NetworkInterface @interface, IEnumerable nameServers) + private static void SetNameServers(NetworkInterface @interface, IEnumerable nameServers) { - var index = @interface.GetIPProperties().GetIPv4Properties().Index; - using var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration"); - foreach (ManagementObject adapter in wmi.GetInstances()) + Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all"); + foreach (var address in nameServers) { - if ((int)(uint)adapter["InterfaceIndex"] == index) - { - var inParams = adapter.GetMethodParameters("SetDNSServerSearchOrder"); - inParams["DNSServerSearchOrder"] = nameServers.Select(item => item.ToString()).ToArray(); - adapter.InvokeMethod("SetDNSServerSearchOrder", inParams, null); - return true; - } + Netsh($@"interface ipv4 add dns ""{@interface.Name}"" {address} validate=no"); } - return false; + static void Netsh(string arguments) + { + var netsh = new ProcessStartInfo + { + FileName = "netsh.exe", + Arguments = arguments, + CreateNoWindow = true, + UseShellExecute = false, + WindowStyle = ProcessWindowStyle.Hidden + }; + Process.Start(netsh)?.WaitForExit(); + } } } } \ No newline at end of file