恢复Netsh,因为WMI引起发布时致命错误(https://github.com/dotnet/runtime/issues/57406)
This commit is contained in:
parent
70bbacde13
commit
d73701340e
@ -7,7 +7,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="DNS" Version="6.1.0" />
|
<PackageReference Include="DNS" Version="6.1.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||||
<PackageReference Include="System.Management" Version="5.0.0" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Management;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
@ -94,23 +94,26 @@ namespace FastGithub.Dns
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="interface"></param>
|
/// <param name="interface"></param>
|
||||||
/// <param name="nameServers"></param>
|
/// <param name="nameServers"></param>
|
||||||
/// <returns></returns>
|
private static void SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
||||||
private static bool SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
|
||||||
{
|
{
|
||||||
var index = @interface.GetIPProperties().GetIPv4Properties().Index;
|
Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all");
|
||||||
using var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
foreach (var address in nameServers)
|
||||||
foreach (ManagementObject adapter in wmi.GetInstances())
|
|
||||||
{
|
{
|
||||||
if ((int)(uint)adapter["InterfaceIndex"] == index)
|
Netsh($@"interface ipv4 add dns ""{@interface.Name}"" {address} validate=no");
|
||||||
{
|
|
||||||
var inParams = adapter.GetMethodParameters("SetDNSServerSearchOrder");
|
|
||||||
inParams["DNSServerSearchOrder"] = nameServers.Select(item => item.ToString()).ToArray();
|
|
||||||
adapter.InvokeMethod("SetDNSServerSearchOrder", inParams, null);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user