接着上次的小工具启用禁用外网,也不是什么高技术含量的,拿来用就行了,所以这里就不多说明,代码里有简单介绍
启用禁用外网方法
public bool ForbiddenNetWork(bool isForbidden) { try { //禁用dns if (isForbidden) { //如果双网卡:114.114.114.114|115.115.115.115|116.116.116.116 8.8.8.8 List<string[]> getDNSList = NetWorkSettingHelper.GetDNS(); if (getDNSList.Count <= 0) { VertifyPwd.isWlan = !setConfig.isForbiddenNetWork; ErrorCollectHelper.InfoLog("禁用DNS", "获取DNS为空,不做任何设置"); return false; } //禁用dns List<string[]> newgateways = new List<string[]>(); for (int j = 0; j < getDNSList.Count; j++) { //strDns网卡:114.114.114.114|115.115.115.115|116.116.116.116 string[] strDns = getDNSList[j]; //禁用dns string[] forbiddenDns = new string[strDns.Length]; for (int i = 0; i < strDns.Length; i++) { //dns:1.1.1.1 forbiddenDns[i] = string.Format("{0}.{0}.{0}.{0}", i + 1); } //需要设置禁用的dns为1.1.1.1,默认不处理 if (string.Equals(string.Join("|", strDns), string.Join("|", forbiddenDns), StringComparison.OrdinalIgnoreCase)) { VertifyPwd.isWlan = !setConfig.isForbiddenNetWork; ErrorCollectHelper.InfoLog("禁用DNS", "DNS为1.1.1.1等默认值,不做任何设置"); return false; } else//保存dns状态,可能多网卡,目前最多考虑双网卡 { if (j == 0) { setConfig.dns0 = string.Join("|", strDns); } else if (j == 1) { setConfig.dns1 = string.Join("|", strDns); } } //禁用dns newgateways.Add(forbiddenDns); } NetWorkSettingHelper.SetDNS(newgateways); //保存dns setConfig.isForbiddenNetWork = true; VertifyPwd.isWlan = false; setConfig.Save(); } else//启用dns { //默认最多考虑双网卡 List<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("启用DNS", "没有找到保存的DNS,无法启用"); return false; } } return true; } catch (Exception ex) { ErrorCollectHelper.ErrorLog("设置网络访问错误", ex.ToString()); return false; } }
网络设置的一个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) { //如果没有启用IP设置的网络设备则跳过 if (!(bool)mo["IPEnabled"]) continue; //设置网关地址 if (getways.Count > 0) { //如果dns为空,则if判断永远为false,因此注释 //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 nics = mc.GetInstances(); List<string[]> getways = new List<string[]>(); foreach (ManagementObject nic in nics) { 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; } } }
初始默认dns存在配置文件里,setConfig就是那个存储的
时间: 2024-11-05 21:48:01