简体   繁体   中英

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. 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. 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. AFAIK, another problem is that it does not support managing IPv6 settings.

So I decided to write to Windows registry directly:

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.

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.

What about NETSH tool? Run it under Administrator.

Process.Start("netsh", "interface ip set dns \"NETWORK NAME\" static 8.8.8.8").WaitForExit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM