程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 啟用禁用外網,啟用禁用

啟用禁用外網,啟用禁用

編輯:C#入門知識

啟用禁用外網,啟用禁用


接著上次的小工具啟用禁用外網,也不是什麼高技術含量的,拿來用就行了,所以這裡就不多說明,代碼裡有簡單介紹

啟用禁用外網方法

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; } } View Code

網絡設置的一個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; } } } View Code

初始默認dns存在配置文件裡,setConfig就是那個存儲的

(上一個是啟用禁用USB)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved