標籤:ext bsp collect timer cat blog 檔案下載 語言 tostring
C#語言實現定時開啟/禁用網卡
- 程式運行
- 程式實現主要代碼
- 原始碼工程檔案(VS2013工程檔案編譯通過)
- 查看網卡名稱附圖
1.win7旗艦版運行:
2.程式實現主要代碼:
/// 網卡列表
1 /// <summary> 2 /// 網卡列表 3 /// </summary> 4 public void NetWorkList() 5 { 6 string manage = "SELECT * From Win32_NetworkAdapter"; 7 ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage); 8 ManagementObjectCollection collection = searcher.Get(); 9 List<string> netWorkList = new List<string>();10 11 foreach (ManagementObject obj in collection)12 {13 netWorkList.Add(obj["Name"].ToString());14 15 }16 this.cmbNetWork.DataSource = netWorkList;17 18 }
/// 禁用網卡
1 /// <summary> 2 /// 禁用網卡 3 /// </summary>5 4 /// <param name="netWorkName">網卡名</param> 5 /// <returns></returns> 6 public bool DisableNetWork(ManagementObject network) 7 { 8 try 9 {10 network.InvokeMethod("Disable", null);11 return true;12 }13 catch14 {15 return false;16 }17 }
/// 啟用網卡
1 /// <summary> 2 /// 啟用網卡 3 /// </summary> 4 /// <param name="netWorkName">網卡名</param> 5 /// <returns></returns> 6 public bool EnableNetWork(ManagementObject network) 7 { 8 try 9 {10 network.InvokeMethod("Enable", null);11 return true;12 }13 catch14 {15 return false;16 }17 18 }
/// 網卡狀態
1 /// <summary> 2 /// 網卡狀態 3 /// </summary> 4 /// <param name="netWorkName">網卡名</param> 5 /// <returns></returns> 6 public bool NetWorkState(string netWorkName) 7 { 8 string netState = "SELECT * From Win32_NetworkAdapter"; 9 ManagementObjectSearcher searcher = new ManagementObjectSearcher(netState);10 ManagementObjectCollection collection = searcher.Get();11 foreach (ManagementObject manage in collection)12 {13 if (manage["Name"].ToString() == netWorkName)14 {15 return true;16 }17 }18 return false;19 }
/// 得到指定網卡
1 /// <summary> 2 /// 得到指定網卡 3 /// </summary> 4 /// <param name="networkname">網卡名字</param> 5 /// <returns></returns> 6 public ManagementObject NetWork(string networkname) 7 { 8 string netState = "SELECT * From Win32_NetworkAdapter"; 9 10 ManagementObjectSearcher searcher = new ManagementObjectSearcher(netState);11 ManagementObjectCollection collection = searcher.Get();12 13 foreach (ManagementObject manage in collection)14 {15 if (manage["Name"].ToString() == networkname)16 {17 return manage;18 }19 }20 21 return null;22 }
/// 開啟網卡
1 /// <summary> 2 /// 開啟網卡 3 /// </summary> 4 public bool startNetWork() 5 { 6 if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) 7 { 8 if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) 9 {10 this.InsertLog("開啟網卡失敗!");11 return false;12 }13 else14 {15 this.InsertLog("開啟網卡成功!");16 return true;17 }18 }19 else20 {21 this.InsertLog("網卡己開啟!");22 return false;23 }24 25 //NetWorkList();26 }
///關閉網卡
1 /// <summary> 2 /// 關閉網卡 3 /// </summary> 4 public bool StopNewWork() 5 { 6 if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) 7 { 8 if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) 9 {10 this.InsertLog("禁用網卡失敗!");11 return false;12 }13 else14 {15 this.InsertLog("禁用網卡成功!");16 return true;17 }18 }19 else20 {21 this.InsertLog("網卡己禁用!");22 return false;23 }24 25 //NetWorkList();26 }
///日誌記錄
1 /// <summary> 2 /// 日誌記錄 3 /// </summary> 4 /// <param name="msg"></param> 5 public void InsertLog(string msg) 6 { 7 if (this.richTextBox1.Lines.Count() > 100) 8 { 9 richTextBox1.Clear();10 }11 string str = String.Format("[{0}]--[{1}]-->{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), this.cmbNetWork.Text.Trim(), msg) + Environment.NewLine;12 richTextBox1.Text = richTextBox1.Text.Insert(0, str);13 }
///定時開啟或者關閉
1 private int startTime = 0; 2 3 /// <summary> 4 /// 定時開啟或者關閉 5 /// </summary> 6 /// <param name="sender"></param> 7 /// <param name="e"></param> 8 private void timer1_Tick(object sender, EventArgs e) 9 {10 if (startTime != time)11 {12 InsertLog((time-startTime - 1) +"秒後執行");13 startTime++;14 return;15 }16 17 startTime = 0;18 if (_state == 0) //0表示開啟狀態,1表示關閉狀態19 {20 if (this.StopNewWork())21 {22 _state = 1;23 }24 }25 else26 {27 if (this.startNetWork())28 {29 _state = 0;30 }31 }32 }
3.工程源檔案下載
附件:工程源檔案 http://files.cnblogs.com/files/JiYF/NetWorkCardConnect.rar
4.查看網卡名稱
C#語言實現定時開啟或禁用網卡小程式