ChangeIP.Source.SetIpAddress.setDns C# (CSharp) Method

setDns() public method

public setDns ( string mainDns, string SubDns ) : void
mainDns string
SubDns string
return void
        public void setDns(string mainDns, string SubDns)
        {
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objMOC = objMC.GetInstances();
            string[] sIPs = { mainDns, SubDns };

            foreach (ManagementObject objMO in objMOC)
            {
                if (!(bool)objMO["IPEnabled"])
                    continue;
                try
                {
                    ManagementBaseObject objDNS = null;

                    objDNS = objMO.GetMethodParameters("SetDNSServerSearchOrder");
                    objDNS["DNSServerSearchOrder"] = sIPs;
                    objMO.InvokeMethod("SetDNSServerSearchOrder", objDNS, null);

                    Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
                    //MessageBox.Show("DNSChanged");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to Set DNS : " + ex.Message);
                    //MessageBox.Show("DNSnotchanged");
                }
            }
        }

Usage Example

Example #1
0
 private bool ChangeDns()
 {
     SetIpAddress setIp = new SetIpAddress();
     if ((isInputIpOk(this.mainDns, rexType.ipAddr)) && (isInputIpOk(this.subDns, rexType.ipAddr)))
     {
         setIp.setDns(this.mainDns, this.subDns);
         return true;
     }
     else
         return false;
 }