将 DNS 设置为“自动获取”以编程方式
在 .NET 上使用 C#,如何将 DNS 服务器设置为“自动获取”?我可以像这样将 IP 地址设置为所需的值。
ManagementClass mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection mObjCol = mClass.GetInstances();
foreach (ManagementObject mObj in mObjCol)
{
if ((bool)mObj["IPEnabled"])
{
ManagementBaseObject mboDNS = mObj.GetMethodParameters("SetDNSServerSearchOrder");
if (mboDNS != null)
{
//Assume X.X.X.X and X.X.X.X are the IPs.
string[] sIPs = { "X.X.X.X", "X.X.X.X" };
mboDNS["DNSServerSearchOrder"] = sIPs;
mObj.InvokeMethod("SetDNSServerSearchOrder", mboDNS, null);
}
}
}
我尝试将两个 IP 设置为 null,例如 sIps = { null, null };,但最终根本没有更改设置。
Using C# on .NET, how do I set the DNS servers to 'Obtain automatically'? I can set the IP addresses to desired values like this.
ManagementClass mClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection mObjCol = mClass.GetInstances();
foreach (ManagementObject mObj in mObjCol)
{
if ((bool)mObj["IPEnabled"])
{
ManagementBaseObject mboDNS = mObj.GetMethodParameters("SetDNSServerSearchOrder");
if (mboDNS != null)
{
//Assume X.X.X.X and X.X.X.X are the IPs.
string[] sIPs = { "X.X.X.X", "X.X.X.X" };
mboDNS["DNSServerSearchOrder"] = sIPs;
mObj.InvokeMethod("SetDNSServerSearchOrder", mboDNS, null);
}
}
}
I've tried setting both IPs to null, like sIps = { null, null };
, but that ends up not changing the settings at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 DNSServerSearchOrder 设置为 null,而不是使用 null 字符串数组。
Try setting DNSServerSearchOrder to null instead of using an array of null strings.