c#.net启用和禁用本地连接,在线等!。 本帖最后由 happydayandday 于 2009-12-14 09:03:57 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在.net写一个cmd的Process,运行ping,分析数据timeout的话运行ipconfig /release和ipconfig /renew 没试过,你应该可以在下面的文章中找到答案。http://msdn.microsoft.com/en-us/library/aa394595(VS.85).aspx 刚ipconfig /release 发现本地连接没有禁用,此方法估计无效 private void button4_Click(object sender, EventArgs e) {NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { string str = String.Format( "Name:\n\t{0}\nDescription:\n\t{1}\nType:\n\t{2}\nStatus:\n\t{3}\nSpeed:\n\t{4}kbps\nSupport Multicast:\n\t{5}", ni.Name, ni.Description, ni.NetworkInterfaceType, ni.OperationalStatus, ni.Speed / 1024, ni.SupportsMulticast); MessageBox.Show(str); } }可以获取所有网卡的信息,其中 OperationalStatus 可判断连接状态, 你要操作连接可以使用Shell32public bool SetNetworkAdapter(bool status) { const string discVerb = "停用(&B)"; const string connVerb = "启用(&A)"; const string network = "网络连接"; string networkConnection = "本地连接"; NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + ni.Id + "\\Connection"; Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false); if (rk != null) { // 区分 PnpInstanceID // 如果前面有 PCI 就是本机的真实网卡 // MediaSubType 为 01 则是常见网卡,02为无线网卡。 string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString(); int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0)); if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI") { networkConnection = ni.Name; } } } string sVerb; if (status) { sVerb = connVerb; } else { sVerb = discVerb; } Shell32.Shell sh = new Shell32.Shell(); Shell32.Folder folder; folder = sh.NameSpace(3); 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.IndexOf(networkConnection) > -1) { //找本地连接的所有右键功能菜单 foreach (Shell32.FolderItemVerb Fib in fi.Verbs()) { if (Fib.Name == sVerb) { Fib.DoIt(); break; } } } } } } } catch (Exception e) { e.ToString(); return false; } return true;private void button3_Click(object sender, EventArgs e) {SetNetworkAdapter(true);//启用} } 经本地测试 SetNetworkAdapter(true);//启用 SetNetworkAdapter(false);//禁用都没有什么反应显示信息中有如下Name: MS TCP Loopback interface Description: MS TCP Loopback interface Type: Loopback Status:UpSpeed: 9765kbps Support Multicast:True没有OperationalStatus 想 lz只获取到MS TCP Loopback interface的信息 而没获取到 本地连接 和本地连接1的信息吗? Status:Up -----------不就是 OperationalStatus NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN) { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 } }禁用启用已经可以了就差第一步.检测外网的一个网站,如www.baidu.com,如果ping不同则实现第2步操作(假死后,估计没法正确读取本地连接的状态) 请写出代码,怎么知道无法连外网 第一步.检测外网的一个网站,如www.baidu.com[DllImport("wininet.dll")] private extern static bool InternetCheckConnection(String url, int flag, int ReservedValue); 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; }第2步操作 本地连接为例string str = null; NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { if (ni.Name == "本地连接") { if (ni.OperationalStatus.ToString() == "Up") { str = "已连接本地连接"; } else if (ni.OperationalStatus.ToString() == "Down") { str = "本地连接断开"; } } } [DllImport("wininet.dll")] 怎么引用,我这无效 using System.Runtime.InteropServices; 不知道为什么 private void Start() { NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection; foreach (INetConnection connection in connections) { INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection); if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN && connProps.Name == "本地连接") { connection.Disconnect(); //禁用网络 connection.Connect(); //启用网络 } } }对独立网卡的无效,服务器测试不行,本地可以 asp.net无法更改文本框的值 实现选择列表 带下拉框的文本输入框,同时还支持模糊查询?如何实现 vs2005下编写的网站,为什么在2000的机器上不能运行? ora-12560错误,请高手指点,急等 挺简单又挺奇怪的问题,大家来帮帮我 XML文档中如何使用中文 求救!!关于数据表更新的错误---大哥救命啊! 我是Asp.Net初学者,请部这个错误怎么回事? 怎样在ASP中实现播放。AVI文件?急!最好有源代码! 您眼中的js对象 与函数 求进阶书籍推荐
http://msdn.microsoft.com/en-us/library/aa394595(VS.85).aspx
private void button4_Click(object sender, EventArgs e)
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
string str = String.Format(
"Name:\n\t{0}\nDescription:\n\t{1}\nType:\n\t{2}\nStatus:\n\t{3}\nSpeed:\n\t{4}kbps\nSupport Multicast:\n\t{5}",
ni.Name, ni.Description, ni.NetworkInterfaceType,
ni.OperationalStatus, ni.Speed / 1024, ni.SupportsMulticast);
MessageBox.Show(str);
}
}可以获取所有网卡的信息,其中 OperationalStatus 可判断连接状态,
你要操作连接可以使用Shell32public bool SetNetworkAdapter(bool status)
{
const string discVerb = "停用(&B)";
const string connVerb = "启用(&A)";
const string network = "网络连接";
string networkConnection = "本地连接";
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + ni.Id + "\\Connection";
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
// 区分 PnpInstanceID
// 如果前面有 PCI 就是本机的真实网卡
// MediaSubType 为 01 则是常见网卡,02为无线网卡。
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 &&
fPnpInstanceID.Substring(0, 3) == "PCI")
{
networkConnection = ni.Name;
}
}
} string sVerb; if (status)
{
sVerb = connVerb;
}
else
{
sVerb = discVerb;
} Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder folder;
folder = sh.NameSpace(3);
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.IndexOf(networkConnection) > -1)
{
//找本地连接的所有右键功能菜单
foreach (Shell32.FolderItemVerb Fib in fi.Verbs())
{
if (Fib.Name == sVerb)
{
Fib.DoIt();
break;
}
}
}
}
}
}
}
catch (Exception e)
{
e.ToString();
return false;
}
return true;
private void button3_Click(object sender, EventArgs e)
{
SetNetworkAdapter(true);//启用
} }
都没有什么反应显示信息中有如下Name:
MS TCP Loopback interface Description:
MS TCP Loopback interface Type: Loopback
Status:Up
Speed: 9765kbps Support
Multicast:True没有OperationalStatus 想
-----------
不就是 OperationalStatus
foreach (INetConnection connection in connections)
{
INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN)
{
connection.Disconnect(); //禁用网络
connection.Connect(); //启用网络
}
}
禁用启用已经可以了就差第一步.检测外网的一个网站,如www.baidu.com,如果ping不同则实现第2步操作(假死后,估计没法正确读取本地连接的状态)
请写出代码,怎么知道无法连外网
private extern static bool InternetCheckConnection(String url, int flag, int ReservedValue);
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;
}
第2步操作 本地连接为例string str = null;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
if (ni.Name == "本地连接")
{
if (ni.OperationalStatus.ToString() == "Up")
{
str = "已连接本地连接";
}
else if (ni.OperationalStatus.ToString() == "Down")
{
str = "本地连接断开";
}
} }
{
NetSharingManagerClass netSharingMgr = new NetSharingManagerClass(); INetSharingEveryConnectionCollection connections = netSharingMgr.EnumEveryConnection;
foreach (INetConnection connection in connections)
{
INetConnectionProps connProps = netSharingMgr.get_NetConnectionProps(connection);
if (connProps.MediaType == tagNETCON_MEDIATYPE.NCM_LAN && connProps.Name == "本地连接")
{
connection.Disconnect(); //禁用网络
connection.Connect(); //启用网络
}
}
}
对独立网卡的无效,服务器测试不行,本地可以