How to check if DNS server is set to 'obtain automatically'

The only way I found is to read from the registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\\{Network_Adaptor_GUID}\NameServer

If NameServer is empty - then DNS is dynamic, otherwise - static.


Vad's answer saved me a ton of time hunting for a solution. Here's some C# if anyone wants to see a very basic implementation.

using Microsoft.Win32;
//...
private void DNSAutoOrStatic(string NetworkAdapterGUID)
        {
            string path = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\" + NetworkAdapterGUID;
            string ns = (string)Registry.GetValue(path, "NameServer", null);
            if (String.IsNullOrEmpty(ns))
            {
                Console.WriteLine("Dynamic DNS");
            }
            else
            {
                Console.WriteLine("Static DNS: " + ns);
            }
        }

You can get the network adapter GUID following these examples.

It's the value of the Id property in System.Net.NetworkInformation.NetworkInterface

Tags:

C#

Dns

Wmi