C#语言实现定时开启/禁用网卡

  1. 程序运行效果图
  2. 程序实现主要代码
  3. 源代码工程文件(VS2013工程文件编译通过)
  4. 查看网卡名称附图

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 catch 14  { 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 catch 14  { 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 else 14  { 15 this.InsertLog("开启网卡成功!"); 16 return true; 17  } 18  } 19 else 20  { 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 else 14  { 15 this.InsertLog("禁用网卡成功!"); 16 return true; 17  } 18  } 19 else 20  { 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 else 26  { 27 if (this.startNetWork()) 28  { 29 _state = 0; 30  } 31  } 32 }

 

3.工程源文件下载

附件:工程源文件  http://files.cnblogs.com/files/JiYF/NetWorkCardConnect.rar 

4.查看网卡名称

转载于:https://www.cnblogs.com/JiYF/p/6104006.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐