C# 启用或禁用网卡 2种方法

最近网络老是不太好,下东西时总是断线,所以就想做一个自动连接的程序,在网上搜了一上,就把代理记录下来了,希望对大家有帮助

[DllImport("wininet.dll")]
private extern static bool InternetCheckConnection(String url, int flag, int ReservedValue);
/// <summary>
/// 第一步.检测外网的一个网站,如www.baidu.com
/// </summary>
/// <returns></returns>
public bool GetExtranet()
{
bool extranet = false;
try
{
if (InternetCheckConnection("http://www.baidu.com/", 1, 0).Equals(false))
{
extranet = false;
}
else
{
extranet = true;
}
}
catch (Exception e)
{
e.ToString();
}
return extranet;
}
1
//禁用 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; 
}
======
2
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();
}
}
}

 

 

posted @ 2013-03-30 17:43  你约我交友网  阅读(1991)  评论(0编辑  收藏  举报