Enable and disable Internet

Source: Internet
Author: User

Enable and disable Internet

The last time the tool was used to disable the Internet, it was not a high-tech one. You just need to use it, so I will not describe it here. There is a brief introduction in the code.

Enable Disable Internet

Public bool ForbiddenNetWork (bool isForbidden) {try {// disable dns if (isForbidden) {// if the dual Nic is: enabled | disabled | ipv8.8.8.8 List <string []> getDNSList = NetWorkSettingHelper. getDNS (); if (getDNSList. count <= 0) {VertifyPwd. isWlan =! SetConfig. isForbiddenNetWork; ErrorCollectHelper. infoLog ("disable DNS", "retrieve DNS is empty, no settings are made"); return false ;} // disable dns List <string []> newgateways = new List <string []> (); for (int j = 0; j <getDNSList. count; j ++) {// strDns NIC: 114.114.114.114 | 115.115.115.115 | 116.116.116.116 string [] strDns = getDNSList [j]; // disable dns string [] forbiddenDns = new string [strDns. length]; for (int I = 0; I <strDns. length; I ++) {// d Ns: 1.1.1.1 forbiddenDns [I] = string. format ("{0 }. {0 }. {0 }. {0} ", I + 1);} // you need to set the disabled dns to 1.1.1.1. if (string. equals (string. join ("|", strDns), string. join ("|", forbiddenDns), StringComparison. ordinalIgnoreCase) {VertifyPwd. isWlan =! SetConfig. isForbiddenNetWork; ErrorCollectHelper. infoLog ("disable DNS", "DNS is the default value such as 1.1.1.1, without any Settings"); return false;} else // Save the dns status. Multiple NICs may exist, currently, up to two NICs can be considered {if (j = 0) {setConfig. dns0 = string. join ("|", strDns);} else if (j = 1) {setConfig. dns1 = string. join ("|", strDns) ;}// disable dns newgateways. add (forbiddenDns);} NetWorkSettingHelper. setDNS (newgateways); // save dns setConfig. isForbiddenNetWork = true; Vertify Pwd. isWlan = false; setConfig. save ();} else // Enable dns {// by default, the List of Dual-network interfaces <string []> getDNSList = new List <string []> (); if (! String. IsNullOrEmpty (setConfig. dns0) {getDNSList. Add (setConfig. dns0.Split ('|');} if (! String. isNullOrEmpty (setConfig. dns1) {getDNSList. add (setConfig. dns1.Split ('|');} if (getDNSList. count> 0) {NetWorkSettingHelper. setDNS (getDNSList); setConfig. isForbiddenNetWork = false; VertifyPwd. isWlan = true; setConfig. save (); return true;} else {VertifyPwd. isWlan =! SetConfig. isForbiddenNetWork; ErrorCollectHelper. infoLog ("Enable DNS", "saved DNS not found, cannot be enabled"); return false ;}return true ;} catch (Exception ex) {ErrorCollectHelper. errorLog ("setting network access errors", ex. toString (); return false ;}}View Code

Network SettingsOne Helper

Public class NetWorkSettingHelper {public static void SetDNS (List <string []> getways) {try {ManagementClass wmi = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = wmi. getInstances (); ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; int n = 0; foreach (ManagementObject mo in moc) {// skip if (! (Bool) mo ["IPEnabled"]) continue; // set the gateway address if (getways. count> 0) {// if dns is empty, if is always false, so note // if (mo ["DNSServerSearchOrder"]! = Null & (mo ["DNSServerSearchOrder"] as String []) [0]! = Null) // {inPar = mo. getMethodParameters ("SetDNSServerSearchOrder"); inPar ["DNSServerSearchOrder"] = getways [n ++]; outPar = mo. invokeMethod ("SetDNSServerSearchOrder", inPar, null); //} mo. clone () ;}} catch (Exception ex) {throw ex ;}} public static List <string []> GetDNS () {try {ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection criteria = Mc. getInstances (); List <string []> getways = new List <string []> (); foreach (ManagementObject nic in progress) {if (Convert. toBoolean (nic ["ipEnabled"]) {if (nic ["DNSServerSearchOrder"]! = Null & (nic ["DNSServerSearchOrder"] as String []) [0]! = Null) {getways. add (nic ["DNSServerSearchOrder"] as String []);} nic. clone () ;}} return getways;} catch (Exception ex) {throw ex ;}}}View Code

By default, dns is stored in the configuration file.

(Enable or disable USB)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.