Mariusz Schimke
Mariusz Schimke

Reputation: 3295

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.

Upvotes: 1

Views: 284

Answers (1)

Woldemar89
Woldemar89

Reputation: 672

What about NETSH tool? Run it under Administrator.

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

Upvotes: 2

Related Questions