1

I would like to modify network adapter DNS servers on Windows programmatically. I don't want to use WMI, because it fails for instance when no network cable is connected to the ethernet adapter being modified. What's more, it does not support setting IPv6 configuration.

Therefore I attempted to set DNS servers directly in the system registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip[6]\Parameters\Interfaces\[Interface GUID]\NameServer

Unfortunately, these settings are not applied instantly.

Netsh command seems to do what I want perfectly, but honestly, I think it would be better if I could stay away from starting external processes from the application I build. I would like it to be independent. I'm wondering if there is a way to do that. Maybe some other registry changes should be modified as well, or some action should be triggered by WMI?

Mariusz Schimke
  • 155
  • 1
  • 8

1 Answers1

3

NETSH is still what you want to do. You seem to have developed some kind of application, but altering the registry is sure not the way to go.

Besides that - I highly recommend you use DHCP and reservation rather then hard coding any IP settings every on a server / client - besides those few that have to be (DHCP servers, Domain Controllers e.g.).

DHCP allows you to centrally manage those settings and roll out to the DHCP clients (Client/Server OS) without ever touching them..

Your only other option would be PowerShell - but again - you don't seem to want to use "external" programs, though - NETSH is a part of Windows, only that it is a command line tool (more or less).

You don't mention much about the developing platform you are using - but there is another chance that e.g. a .NET FrameWork provider lets you enumerate and change the settings of network cards, I never looked in to that specifically, but I could imagine there is a chance you can accomplish this..

And if you are afraid NETSH would fail - well - you can still execute it and afterwards double check the registry if the expected settings had been applied..

  • Thank you for your response. The thing is that the sole purpose of the application I'm developing is to change DNS servers of all network adapters. I haven't found any solution to do that based purely on .NET Framework. That's why I need opinions and ideas now, so thank you for this one, it's really valuable! – Mariusz Schimke Jan 30 '19 at 07:58
  • You could use you local (or remote) group policy setting. – bjoster Feb 03 '19 at 16:11