Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

dns

wmi

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'?

like image 684
Micah Avatar asked Nov 13 '12 19:11

Micah


1 Answers

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.

like image 195
Vad Avatar answered Sep 28 '22 00:09

Vad