代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
启用或禁用网卡
作者:
dezai
/ 发布于
2014/1/26
/
333
方法一、 //禁用 SetNetworkAdapter(False) //启用 SetNetworkAdapter(True) //添加引用system32\shell32.dll private static bool SetNetworkAdapter(bool status) { const string discVerb = "停用(&B)"; // "停用(&B)"; const string connVerb = "启用(&A)"; // "启用(&A)"; const string network = "网络连接"; //"网络连接"; const string networkConnection = "VMware Network Adapter VMnet1"; // "本地连接" string sVerb = null; if (status) { sVerb = connVerb; } else { sVerb = discVerb; } Shell32.Shell sh = new Shell32.Shell(); Shell32.Folder folder = sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfCONTROLS); try { //进入控制面板的所有选项 foreach (Shell32.FolderItem myItem in folder.Items()) { //进入网络连接 if (myItem.Name == network) { Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder; foreach (Shell32.FolderItem fi in fd.Items()) { //找到本地连接 if ((fi.Name == networkConnection)) { //找本地连接的所有右键功能菜单 foreach (Shell32.FolderItemVerb Fib in fi.Verbs()) { if (Fib.Name == sVerb) { Fib.DoIt(); return true; } } } } } } } catch (Exception e) { Console.WriteLine(e.Message); return false; } return true; } ================================================================================== 方法二 、 c#中禁用和开启网卡 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Management; using System.Collections; namespace NetWorkControl { public partial class Form1 : Form { public Form1() { InitializeComponent(); NetWorkList(); } /// <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; } private void btnStart_Click(object sender, EventArgs e) { if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) { if (!EnableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) { MessageBox.Show("开启网卡失败!"); } else { MessageBox.Show("开启网卡成功!"); } } else { MessageBox.Show("网卡己开启!"); } NetWorkList(); } private void btnStop_Click(object sender, EventArgs e) { if (NetWorkState(this.cmbNetWork.SelectedValue.ToString())) { if (!DisableNetWork(NetWork(this.cmbNetWork.SelectedValue.ToString()))) { MessageBox.Show("禁用网卡失败!"); } else { MessageBox.Show("禁用网卡成功!"); } } else { MessageBox.Show("网卡己禁用!"); } NetWorkList(); } } }
试试其它关键字
启用或禁用网卡
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3