12

When I get my servers DNS settings using the DNSServerSearchOrder property of my network card's settings, it returns the DNS server that it automatically resolves to, rather than a value that would indicate it is dynamic (such as null).

for example, to set my DNS servers to 'Obtain Automatically' I do:

ManagementBaseObject newDNS = myNICManagementObject.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = null;
ManagementBaseObject setDNS = myNICManagementObject.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);

Now, after I have set it to 'Obtain Automatically' with the other command I want to confirm it was set:

if( myNICManagementObject["DNSServerSearchOrder"] == null )
{
    MessageBox.Show("DNS Servers Set to Dynamic!");
}

However, the above code does not return null (nor pop-up a messagebox) as expected. Instead it returns the DNS server that it dynamically figures out from my ISP.

Is there a way to determine programmatically that my DNS servers are set to 'Obtain Automatically'?

Micah
  • 429
  • 1
  • 5
  • 11
  • I read that post before I asked my question and have now re-read it but still do not see anything that can answer my question – Micah Nov 13 '12 at 23:46
  • 2
    I also have the same question. I think that there must be an way to get this information. If you type in `netsh interface ip show config` then you'll see, that netsh is able to differentiate the source of the dns servers. I even thought of parsing the results from netsh, but this doesn't work, if someone uses my program with windows installation which has another localization package... – netblognet Feb 26 '13 at 15:52

2 Answers2

8

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.

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
Vad
  • 858
  • 2
  • 7
  • 17
  • This fails for empty manual DNS address. In this scenario netsh correctly identifies the configuration as static (192.168.1.1). – Andrew May 25 '17 at 12:34
  • I'd like to notice that netsh also shows static for L2TP connections. – vadim_hr Feb 26 '19 at 12:13
  • If the user sets the DNS server manually on the Wi-Fi profile rather than on the adapter then NameServer is also empty, but ProfileNameServer is not. – Bryan Larsen Feb 01 '23 at 21:13
1

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

Community
  • 1
  • 1
Rocky
  • 494
  • 6
  • 18