WMI设置DNS
This commit is contained in:
parent
7f00d312ca
commit
19ed7a4537
@ -7,6 +7,7 @@
|
|||||||
<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,26 +94,23 @@ namespace FastGithub.Dns
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="interface"></param>
|
/// <param name="interface"></param>
|
||||||
/// <param name="nameServers"></param>
|
/// <param name="nameServers"></param>
|
||||||
private static void SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
/// <returns></returns>
|
||||||
|
private static bool SetNameServers(NetworkInterface @interface, IEnumerable<IPAddress> nameServers)
|
||||||
{
|
{
|
||||||
Netsh($@"interface ipv4 delete dns ""{@interface.Name}"" all");
|
var index = @interface.GetIPProperties().GetIPv4Properties().Index;
|
||||||
foreach (var address in nameServers)
|
using var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
||||||
|
foreach (ManagementObject adapter in wmi.GetInstances())
|
||||||
{
|
{
|
||||||
Netsh($@"interface ipv4 add dns ""{@interface.Name}"" {address} validate=no");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Netsh(string arguments)
|
return false;
|
||||||
{
|
|
||||||
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