Micah Micah - 10 months ago 44
C# Question

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

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

Vad Vad
Answer Source

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


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