如何检查DNS服务器是否设置为“自动获取" [英] How to check if DNS server is set to 'obtain automatically'

查看:82
本文介绍了如何检查DNS服务器是否设置为“自动获取"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用网卡设置的DNSServerSearchOrder属性获取服务器的DNS设置时,它将返回它自动解析为的DNS服务器,而不是表明它是动态的值(例如null).

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).

例如,要将我的DNS服务器设置为自动获取",

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!");
}

但是,上面的代码没有按预期返回null(也不弹出消息框).相反,它将返回从我的ISP动态找出的DNS服务器.

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.

是否可以通过编程方式确定我的DNS服务器设置为自动获取"?

Is there a way to determine programmatically that my DNS servers are 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

如果 NameServer 为空,则DNS为动态,否则为静态.

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

这篇关于如何检查DNS服务器是否设置为“自动获取"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆