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)