2

我有这个改变DNS的功能:

/**
 * Change dns
 * 
 * @param bool
 */

private void changeDns(bool develop)
{
    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)
            {
                if (develop == true)
                {
                    // Set dns
                    mboDNS["DNSServerSearchOrder"] = this.dnsServer;

                    // Log info
                    foreach (String dns in this.dnsServer) {

                        this.addLog(dns, "DNS servers change to:", Color.Green);

                    }
                }
                else
                {
                    // Set dns
                    mboDNS["DNSServerSearchOrder"] = null;

                    // Log info
                    this.addLog("DHCP", "DNS servers change to:", Color.Green);
                }

                // Change dns
                mObj.InvokeMethod("SetDNSServerSearchOrder", mboDNS, null);
            }
        }
    }
}

但现在我需要另一种方式。win32如何获取dns?

我从 MSDN尝试了这段代码,但它返回了大约 10 个结果。

4

1 回答 1

0

答案是:

    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface ni in nics)
    {
        if (ni.OperationalStatus == OperationalStatus.Up)
        {
            IPAddressCollection ips = ni.GetIPProperties().DnsAddresses;
            foreach (System.Net.IPAddress ip in ips)
            {
                MessageBox.Show(ip.ToString());
            }
        }
    }
于 2013-03-18T09:53:59.473 回答