C#實現定時開啟或禁用網卡小程序
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
C#語言實現定時開啟/禁用網卡
1、程序運行效果圖
2、程序實現主要代碼
3、源代碼工程文件(VS2013工程文件編譯通過)
4、查看網卡名稱附圖
1、win7旗艦版運行效果圖:
2、程序實現主要代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Collections;
namespace NetWorkCardConnect
{
public partial class Form1 : Form
{
private int _state = 0;
private int time = 0;
public Form1()
{
InitializeComponent();
NetWorkList();
timer1.Interval = 1000;//int.Parse(textBox1.Text.Trim()) *
//if(NetWorkState(cmbNetWork.Text.Trim()))
}
/// <summary>
/// 網卡列表
/// </summary>
public void NetWorkList()
{
string manage = "SELECT * From Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);
ManagementObjectCollection collection = searcher.Get();
List<string> netWorkList = new List<string>();
foreach (ManagementObject obj in collection)
{
netWorkList.Add(obj["Name"].ToString());
}
this.cmbNetWork.DataSource = netWorkList;
}
/// <summary>
/// 禁用網卡
/// </summary>5
/// <param name="netWorkName">網卡名</param>
/// <returns></returns>
public bool DisableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Disable", null);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 啟用網卡
/// </summary>
/// <param name="netWorkName">網卡名</param>
/// <returns></returns>
public bool EnableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Enable", null);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 網卡狀態
/// </summary>
/// <param name="netWorkName">網卡名</param>
/// <returns></returns>
public bool NetWorkState(string netWorkName)
{
string netState = "SELECT * From Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(netState);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject manage in collection)
{
if (manage["Name"].ToString() == netWorkName)
{
return true;
}
}
return false;
}
/// <summary>
/// 得到指定網卡
/// </summary>
/// <param name="networkname">網卡名字</param>
/// <returns></returns>
public ManagementObject NetWork(string networkname)
{
string netState = "SELECT * From Win32_NetworkAdapter";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(netState);
ManagementObjectCollection collection = searcher.Get();
foreach (ManagementObject manage in collection)
{
if (manage["Name"].ToString() == networkname)
{
return manage;
}
}
return null;
}
/// <summary>
/// 開啟網卡
/// </summary>
public bool startNetWork()
{
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
{
if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
{
this.InsertLog("開啟網卡失敗!");
return false;
}
else
{
this.InsertLog("開啟網卡成功!");
return true;
}
}
else
{
this.InsertLog("網卡己開啟!");
return false;
}
//NetWorkList();
}
/// <summary>
/// 關閉網卡
/// </summary>
public bool StopNewWork()
{
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
{
if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
{
this.InsertLog("禁用網卡失敗!");
return false;
}
else
{
this.InsertLog("禁用網卡成功!");
return true;
}
}
else
{
this.InsertLog("網卡己禁用!");
return false;
}
//NetWorkList();
}
/// <summary>
/// 日志記錄
/// </summary>
/// <param name="msg"></param>
public void InsertLog(string msg)
{
if (this.richTextBox1.Lines.Count() > 100)
{
richTextBox1.Clear();
}
string str = String.Format("[{0}]--[{1}]-->{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), this.cmbNetWork.Text.Trim(), msg) + Environment.NewLine;
richTextBox1.Text = richTextBox1.Text.Insert(0, str);
}
private int startTime = 0;
/// <summary>
/// 定時開啟或者關閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (startTime != time)
{
InsertLog((time-startTime - 1) +"秒后執行");
startTime++;
return;
}
startTime = 0;
if (_state == 0) //0表示開啟狀態,1表示關閉狀態
{
if (this.StopNewWork())
{
_state = 1;
}
}
else
{
if (this.startNetWork())
{
_state = 0;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
this.InsertLog("網卡開啟中……");
this.startNetWork();
}
private void btnStop_Click(object sender, EventArgs e)
{
this.InsertLog("網卡禁用中……");
this.StopNewWork();
}
private void button1_Click(object sender, EventArgs e)
{
time = int.Parse(textBox1.Text.Trim());
startTime = 0;
if (this.btnTimer.Text.Equals("開啟定時"))
{
timer1.Enabled = true;
this.InsertLog("啟用定時開啟或禁用網卡!");
this.btnTimer.Text = "關閉定時";
}
else
{
timer1.Enabled = false;
this.InsertLog("關閉定時開啟或禁用網卡!");
this.btnTimer.Text = "開啟定時";
}
}
}
}
該文章在 2021/5/22 10:46:23 編輯過 |
關鍵字查詢
相關文章
正在查詢... |