C#语言实现定时开启或禁用网卡小程序

Posted JiYF大男孩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#语言实现定时开启或禁用网卡小程序相关的知识,希望对你有一定的参考价值。

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.查看网卡名称

以上是关于C#语言实现定时开启或禁用网卡小程序的主要内容,如果未能解决你的问题,请参考以下文章

抖音火爆表白小程序(C#语言实现)

C#定时器怎么写?

C#中如何向指定的窗口或程序发送按键?

centos 开启关闭网卡(禁用网卡)

联想笔记本win7系统怎么创建批处理快速开启禁用无线网卡

怎么在linux 系统下,禁用和启用网卡,并看状态,比如像WINDOW XP 下 本地连接,禁用,启用,连接状态,