[英]Network adapter DNS servers not applied instantly when set in Windows registry
I would like to set DNS servers of a network adapter in Windows. 我想在Windows中设置网络适配器的DNS服务器。 Initially, I tried to use WMI for that purpose, but it turns out that it does not allow me to do what I want in some cases. 最初,我尝试将WMI用于此目的,但事实证明,在某些情况下,它不允许我做我想做的事情。 For instance, when I attempt to set DNS servers of an ethernet adapter with no cable connected to it, WMI returns an error saying that IP is not enabled for that adapter. 例如,当我尝试设置没有连接电缆的以太网适配器的DNS服务器时,WMI返回一个错误,指出该适配器未启用IP。 AFAIK, another problem is that it does not support managing IPv6 settings. AFAIK,另一个问题是它不支持管理IPv6设置。
So I decided to write to Windows registry directly: 因此,我决定直接写入Windows注册表:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip[6]\Parameters\Interfaces\[Interface GUID]\NameServer
Unfortunately, when I do that, it seems that the newly written settings are not applied instantly. 不幸的是,当我这样做时,似乎新写入的设置不会立即应用。 Because of that, I use WMI to disable and enable the adapter. 因此,我使用WMI禁用和启用适配器。
This solution, however, seems really cumbersome, and I'm wondering if the same thing can't be done in a 'clean' way. 但是,此解决方案似乎很麻烦,我想知道是否不能以“干净”的方式完成相同的事情。 I'm trying to do that in C#, .NET Framework 4.7.2. 我正在尝试在C#、. NET Framework 4.7.2中做到这一点。
What about NETSH tool? NETSH工具呢? Run it under Administrator. 在管理员下运行它。
Process.Start("netsh", "interface ip set dns \"NETWORK NAME\" static 8.8.8.8").WaitForExit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.