C#實現(xiàn)定時開啟或禁用網(wǎng)卡小程序
當(dāng)前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
C#語言實現(xiàn)定時開啟/禁用網(wǎng)卡
1、程序運行效果圖
2、程序?qū)崿F(xiàn)主要代碼
3、源代碼工程文件(VS2013工程文件編譯通過)
4、查看網(wǎng)卡名稱附圖
1、win7旗艦版運行效果圖:
2、程序?qū)崿F(xiàn)主要代碼:
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>
/// 網(wǎng)卡列表
/// </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>
/// 禁用網(wǎng)卡
/// </summary>5
/// <param name="netWorkName">網(wǎng)卡名</param>
/// <returns></returns>
public bool DisableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Disable", null);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 啟用網(wǎng)卡
/// </summary>
/// <param name="netWorkName">網(wǎng)卡名</param>
/// <returns></returns>
public bool EnableNetWork(ManagementObject network)
{
try
{
network.InvokeMethod("Enable", null);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 網(wǎng)卡狀態(tài)
/// </summary>
/// <param name="netWorkName">網(wǎng)卡名</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>
/// 得到指定網(wǎng)卡
/// </summary>
/// <param name="networkname">網(wǎng)卡名字</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>
/// 開啟網(wǎng)卡
/// </summary>
public bool startNetWork()
{
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
{
if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
{
this.InsertLog("開啟網(wǎng)卡失敗!");
return false;
}
else
{
this.InsertLog("開啟網(wǎng)卡成功!");
return true;
}
}
else
{
this.InsertLog("網(wǎng)卡己開啟!");
return false;
}
//NetWorkList();
}
/// <summary>
/// 關(guān)閉網(wǎng)卡
/// </summary>
public bool StopNewWork()
{
if (NetWorkState(this.cmbNetWork.SelectedValue.ToString()))
{
if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString())))
{
this.InsertLog("禁用網(wǎng)卡失敗!");
return false;
}
else
{
this.InsertLog("禁用網(wǎng)卡成功!");
return true;
}
}
else
{
this.InsertLog("網(wǎng)卡己禁用!");
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>
/// 定時開啟或者關(guān)閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
if (startTime != time)
{
InsertLog((time-startTime - 1) +"秒后執(zhí)行");
startTime++;
return;
}
startTime = 0;
if (_state == 0) //0表示開啟狀態(tài),1表示關(guān)閉狀態(tài)
{
if (this.StopNewWork())
{
_state = 1;
}
}
else
{
if (this.startNetWork())
{
_state = 0;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
this.InsertLog("網(wǎng)卡開啟中……");
this.startNetWork();
}
private void btnStop_Click(object sender, EventArgs e)
{
this.InsertLog("網(wǎng)卡禁用中……");
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("啟用定時開啟或禁用網(wǎng)卡!");
this.btnTimer.Text = "關(guān)閉定時";
}
else
{
timer1.Enabled = false;
this.InsertLog("關(guān)閉定時開啟或禁用網(wǎng)卡!");
this.btnTimer.Text = "開啟定時";
}
}
}
}
該文章在 2021/5/22 10:46:23 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |