Reputation: 439
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'?
Upvotes: 12
Views: 6129
Reputation: 495
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
Upvotes: 1
Reputation: 898
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.
Upvotes: 8