如何在c#中修改DNS IP地址

  • 本文关键字:DNS IP 地址 修改 c# dns
  • 更新时间 :
  • 英文 :


为了在我的WPF应用程序中以编程方式更改我的适配器(NetworkInterface) DNS IP地址,我遵循了这个问题:使用c#在windows中更改DNS答案并添加如下:

/// <summary>
/// Method to set the DNS IP addresses of a given <see cref="NetworkInterface"/>
/// Note : using "Win32_NetworkAdapterConfiguration" library, so only on Windows OS
/// </summary>
/// <param name="ni">The <see cref="NetworkInterface"/> adapter to modify its DNS IP addresses along given <paramref name="addresses"/></param>
/// <param name="addresses">The IP addresses to store in <paramref name="ni"/> adapter as its new DNS IP addresses</param>
public static void SetDNS(this NetworkInterface ni, string[] addresses)
{
if (ni == null) return;
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
if (objMO["Caption"].ToString().Contains(ni.Description))
{
ManagementBaseObject objdns = objMO.GetMethodParameters("SetDNSServerSearchOrder");
if (objdns != null)
{
objdns["DNSServerSearchOrder"] = addresses;
objMO.InvokeMethod("SetDNSServerSearchOrder", objdns, null);
}
}
}
}
}

进入调试模式显示它正确地调用了这个函数,循环通过ManagementObjectCollection,结束其中一个项目以正确匹配我的2if语句,最后调用

objdns["DNSServerSearchOrder"] = addresses;
objMO.InvokeMethod("SetDNSServerSearchOrder", objdns, null);

参见:https://i.stack.imgur.com/hxqhS.jpg

但是当我用ipconfig /allCLI检查时,我的DNS IP地址没有改变!如何通过c#直接在windows中有效地更改我的DNS地址,而不是通过调用cmd.exe进程。

谢谢。

所以这里是我结束改变我的系统DNS地址。感谢@jimi以我的方式帮助我更好地理解这一点。

所以毕竟我使用netsh无声地设置新的DNS地址,正如@jimi指出的,因为IPv6不支持我尝试的原始解决方案(通过WMI)

到目前为止(没有完全测试)它工作!

我是这样做的:

/// <summary>
/// Start a <see cref="Process"/>
/// </summary>
/// <param name="processName">The <see cref="ProcessStartInfo.FileName"/> (usually name of the exe / command to start)</param>
/// <param name="args">The <see cref="ProcessStartInfo.Arguments"/> (the argument of the command)</param>
/// <param name="verb">The <see cref="ProcessStartInfo.Verb"/>. Use "runas" to start the process with admin priviledge (default is null)</param>
/// <param name="useShell">The <see cref="ProcessStartInfo.UseShellExecute"/>. Does the process run silently or not (silent by default)</param>
/// <param name="redirectErros">The <see cref="ProcessStartInfo.RedirectStandardError"/>. Do we redirect standard error ? (true by default)</param>
/// <param name="redirectOutput">The <see cref="ProcessStartInfo.RedirectStandardOutput"/>. Do we redirect standard output ? (true by default)</param>
/// <param name="noWindow">The <see cref="ProcessStartInfo.CreateNoWindow"/>. Do we prevent the creation of CMD window to run silently ? (silent by default)</param>
/// <returns>True if <paramref name="processName"/> isn't null and process execution succeeded. False if <paramref name="processName"/> is null or empty.
/// Throw an <see cref="Exception"/> if execution failed</returns>
public static bool StartProcess(string processName, string args, string verb = null, bool useShell = false, bool redirectErros = true, bool redirectOutput = true, bool noWindow = true)
{
if (string.IsNullOrWhiteSpace(processName))
return false;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = processName;
psi.Arguments = args;
psi.UseShellExecute = useShell;
psi.RedirectStandardOutput = redirectOutput;
psi.RedirectStandardError = redirectErros;
psi.CreateNoWindow = noWindow;
if (verb != null)
psi.Verb = verb;
Process proc = Process.Start(psi);
proc.WaitForExit();
string errors = proc.StandardError.ReadToEnd();
string output = proc.StandardOutput.ReadToEnd();
if (proc.ExitCode != 0)
throw new Exception(processName + " exit code: " + proc.ExitCode.ToString() + " " + (!string.IsNullOrEmpty(errors) ? " " + errors : "") + " " + (!string.IsNullOrEmpty(output) ? " " + output : ""));
return true;
}
/// <summary>
/// Convinient method to start a "netsh" process as admin to set a new DNS IP address calling <see cref="StartProcess(string, string, string, bool, bool, bool)"/>
/// </summary>
/// <param name="interfaceName">The name of the interface to set its new <paramref name="address"/> IP ddress</param>
/// <param name="address">The new IP address to set of the <paramref name="interfaceName"/> DNS</param>
/// <param name="isPrimary">Is this new DNS IP address is a primary one ?</param>
/// <returns><see cref="StartProcess(string, string, string, bool, bool, bool)"/> return value, 
/// or false if <paramref name="address"/> isn't a correct IP address</returns>
public static bool netshSetNewDNS(string interfaceName, string address, bool isPrimary)
{
var ipVer = IPversion(address, RGXVX);
if (!(ipVer[0] || ipVer[1]))
return false;
return netshSetNewDNS(interfaceName, address, isPrimary, ipVer[0]);
}
/// <summary>
/// Convinient method to start a "netsh" process as admin to set a new DNS IP address calling <see cref="netshSetNewDNS(string, string, bool)"/>
/// </summary>
/// <param name="interfaceName">The name of the interface to set its new <paramref name="address"/> IP ddress</param>
/// <param name="address">The new IP address to set of the <paramref name="interfaceName"/> DNS</param>
/// <param name="isPrimary">Is this new DNS IP address is a primary one ?</param>
/// <param name="isIPv6">Does <paramref name="address"/> is IPv6 ?</param>
/// <returns><see cref="netshSetNewDNS(string, string, bool)"/> return value</returns>
public static bool netshSetNewDNS(string interfaceName, string address, bool isPrimary, bool isIPv6)
{            
string arg = string.Format("interface {0} {1} dnsservers "{2}"{3} {4} {5}", isIPv6 ? "ipv6" : "ipv4", isPrimary ? "set" : "add", interfaceName, isPrimary ? " static" : "", address, isPrimary ? "primary" : "index=2");
return StartProcess("netsh", arg, "runas");
}
/// <summary>
/// Method to set the DNS IP addresses of a given <see cref="NetworkInterface"/>
/// note : we use netsh silently.
/// see : https://www.tenforums.com/tutorials/77444-change-ipv4-ipv6-dns-server-address-windows.html
/// </summary>
/// <param name="ni">The <see cref="NetworkInterface"/> adapter to modify its DNS IP addresses along given <paramref name="addresses"/></param>
/// <param name="ipv4">The IPv4 addresses to store in <paramref name="ni"/> adapter as its new DNS IP addresses</param>
/// <param name="ipv6">The IPv6 addresses to store in <paramref name="ni"/> adapter as its new DNS IP addresses</param>
public static void SetDNS(this NetworkInterface ni, string[] ipv4, string[] ipv6)
{
if (!(ipv4.Any(add => IsIP(add, RGXV4)) || ipv6.Any(add => IsIP(add, RGXV6))))
{
Debug.WriteLine("None of the suplied addresses are IPv4/6.nCan not update DNS IP addresses.");
return;
}
// delete current IPv4 DNS
StartProcess("netsh", "interface ipv4 delete dnsservers "" + ni.Name + "" all", "runas");
// delete current IPv6 DNS
StartProcess("netsh", "interface ipv6 delete dnsservers "" + ni.Name + "" all", "runas");
string address;
//set new IPv4 DNS addresses
if (ipv4 != null && ipv4.Length>0)
{                
//primary
address = ipv4[0];                
try
{ bool res = netshSetNewDNS(ni.Name, address, true, false); }
catch(Exception e)
{ Debug.WriteLine(e.Message); }
if (ipv4.Length>1)
{
//secondary
address = ipv4[1];
try
{ bool res = netshSetNewDNS(ni.Name, address, false, false); }
catch (Exception e)
{ Debug.WriteLine(e.Message); }
}
}

//set new IPv6 DNS addresses
if (ipv6 != null && ipv6.Length > 0)
{

//primary
address = ipv6[0];
try
{ bool res = netshSetNewDNS(ni.Name, address, true, true); }
catch (Exception e)
{ Debug.WriteLine(e.Message); }
if (ipv6.Length > 1)
{
//secondary
address = ipv6[1];
try
{ bool res = netshSetNewDNS(ni.Name, address, false, true); }
catch (Exception e)
{ Debug.WriteLine(e.Message); }
}
}
}

我虽然1 "minor"问题:设置ipv6 (+ipv4 ?)地址有性能问题。

而只有新的(2)ipv4 IP地址,它几乎是立即的(最多1秒),与2 ipv6 + 2 ipv4设置,它需要长达10秒之前,实际(正确)改变我的DNS IP地址。

调试显示在StartProcess(...)函数调用的辅助IPv4 DNS地址设置(在SetDNS(...) bool res = netshSetNewDNS(ni.Name, address, false, false);)的proc.WaitForExit();上冻结!

这是奇怪的,它总是冻结在这里,因为当我调用只有2个新的IPv4 DNS地址设置,它没有冻结这次…

相关内容

  • 没有找到相关文章

最新更新