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

http://www.cnblogs.com/freedom831215/archive/2011/08/14/2137828.html


[DllImport("wininet.dll")]
private extern static bool InternetCheckConnection(String url, int flag, int ReservedValue);
///


/// 第一步.检测外网的一个网站,如www.baidu.com
///

///
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();

}

///

/// 网卡列表

///

public void NetWorkList()

{

string manage = "SELECT * From Win32_NetworkAdapter";

ManagementObjectSearcher searcher = new ManagementObjectSearcher(manage);

ManagementObjectCollection collection = searcher.Get();

List netWorkList = new List();

foreach (ManagementObject obj in collection)

{

netWorkList.Add(obj["Name"].ToString());

}

this.cmbNetWork.DataSource = netWorkList;

}

///

/// 禁用网卡

///

5

/// 网卡名

///

public bool DisableNetWork(ManagementObject network)

{

try

{

network.InvokeMethod("Disable", null);

return true;

}

catch

{

return false;

}

}

///

/// 启用网卡

///

/// 网卡名

///

public bool EnableNetWork(ManagementObject network)

{

try

{

network.InvokeMethod("Enable", null );

return true;

}

catch

{

return false;

}

}

///

/// 网卡状态

///

/// 网卡名

///

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;

}

///

/// 得到指定网卡

///

/// 网卡名字

///

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();

}

}

}

你可能感兴趣的:(windows,C#)