今天朋友让我帮他做一个自动禁用/启用网络连接的小软件,因为他的电驴下载东西的时候,网络总断。解决原理可用PING命令不停PING某一网站,当不通时则启用/禁用网络连接。
但是让人郁闷的是,找了一天竟然没发现一个可以让网卡开启和关闭的代码。通过netsh也无法达到效果,XP下使用netsh根本就不能用。郁闷之极。。测试了几段代码,都无效。
打算放弃的时候,发现了一段小程序,使用的是模拟鼠标按键的原理,打开控制面板,获取面板里的列表,找到网络连接并打开,再到想要关闭的相应项中模拟右键和左键。。很多都是硬编码,但是却实现了这个功能!~~
今天朋友让我帮他做一个自动禁用/启用网络连接的小软件,因为他的电驴下载东西的时候,网络总断。解决原理可用PING命令不停PING某一网站,当不通时则启用/禁用网络连接。
但是让人郁闷的是,找了一天竟然没发现一个可以让网卡开启和关闭的代码。通过netsh也无法达到效果,XP下使用netsh根本就不能用。郁闷之极。。测试了几段代码,都无效。
打算放弃的时候,发现了一段小程序,使用的是模拟鼠标按键的原理,打开控制面板,获取面板里的列表,找到网络连接并打开,再到想要关闭的相应项中模拟右键和左键。。很多都是硬编码,但是却实现了这个功能!~~
代码如下:
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Runtime.InteropServices;
namespace ConnectionNetwork
{
public class ToggleNetworkConnection
{
#region "Public Methods"
public static void ToggleWirelessConnection()
{
foreach (Shell32.FolderItemVerb verb in WirelessConnectionFolderItem.Verbs())
{
if (verb.Name == "启用(&A)" || verb.Name == "禁用(&B)")
{
verb.DoIt();
goto endOfForLoop;
}
}
endOfForLoop:
System.Threading.Thread.Sleep(1000);
}
public static void ToggleLocalAreaConnection()
{
foreach (Shell32.FolderItemVerb verb in LocalAreaConnectionFolderItem.Verbs())
{
//FOR 2000
// if (verb.Name == "启用(&A)" || verb.Name == "禁用(&B)")
// {
// verb.DoIt();
// goto endOfForLoop;
// }
// }
//endOfForLoop:
// System.Threading.Thread.Sleep(1000);
if (verb.Name == "启用(&A)" || verb.Name == "停用(&B)")
{
verb.DoIt();
goto endOfForLoop;
}
}
endOfForLoop:
System.Threading.Thread.Sleep(1000);
}
#endregion
#region "Properties"
private static Shell32.Folder ControlPanelFolder
{
get
{
Shell32.Shell shell = new Shell32.Shell();
return shell.NameSpace(3);
}
}
private static Shell32.Folder NetworkFolder
{
get
{
Shell32.Folder retVal = null;
foreach (Shell32.FolderItem fi in ControlPanelFolder.Items())
{
//WindowsXP及以上需将下面的字符串改为:“网络连接”,XP以下为"网络和拨号连接"
if (fi.Name == "网络连接")
{
retVal=(Shell32.Folder)fi.GetFolder;
}
}
if (retVal == null)
{
throw (new NetworkConnectionsFolderNotFoundException());
}
else
{
return retVal;
}
}
}
private static Shell32.FolderItem LocalAreaConnectionFolderItem
{
get
{
Shell32.FolderItem retVal = null;
Properties.Settings set = Properties.Settings.Default;
foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())
{
Console.WriteLine(folderItem.Name);
if (folderItem.Name ==set.networkname)
{
retVal = folderItem;
goto endOfForLoop;
}
}
endOfForLoop:
if (retVal == null)
{
throw (new LocalAreaConnectionFolderItemNotFoundException());
}
else
{
return retVal;
}
}
}
private static Shell32.FolderItem WirelessConnectionFolderItem
{
get
{
Shell32.FolderItem retVal = null;
Properties.Settings set = Properties.Settings.Default;
foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())
{
Console.WriteLine(folderItem.Name);
if (folderItem.Name == set.networkname)
{
retVal = folderItem;
goto endOfForLoop;
}
}
endOfForLoop:
if (retVal == null)
{
throw (new WirelessConnectionFolderItemNotFoundException());
}
else
{
return retVal;
}
}
}
#endregion
#region "Custom Exceptions"
public class NetworkConnectionsFolderNotFoundException : Exception
{
public override string Message
{
get
{
return "The Network Connections Folder Could Not Be Found!";
}
}
}
public class LocalAreaConnectionFolderItemNotFoundException : Exception
{
public override string Message
{
get
{
return "The Local Area Connection Folder Could Not Be Found!";
}
}
}
public class WirelessConnectionFolderItemNotFoundException : Exception
{
public override string Message
{
get
{
return "The Wireless Connection Folder Could Not Be Found!";
}
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Runtime.InteropServices;
namespace ConnectionNetwork
{
public class ToggleNetworkConnection
{
#region "Public Methods"
public static void ToggleWirelessConnection()
{
foreach (Shell32.FolderItemVerb verb in WirelessConnectionFolderItem.Verbs())
{
if (verb.Name == "启用(&A)" || verb.Name == "禁用(&B)")
{
verb.DoIt();
goto endOfForLoop;
}
}
endOfForLoop:
System.Threading.Thread.Sleep(1000);
}
public static void ToggleLocalAreaConnection()
{
foreach (Shell32.FolderItemVerb verb in LocalAreaConnectionFolderItem.Verbs())
{
//FOR 2000
// if (verb.Name == "启用(&A)" || verb.Name == "禁用(&B)")
// {
// verb.DoIt();
// goto endOfForLoop;
// }
// }
//endOfForLoop:
// System.Threading.Thread.Sleep(1000);
if (verb.Name == "启用(&A)" || verb.Name == "停用(&B)")
{
verb.DoIt();
goto endOfForLoop;
}
}
endOfForLoop:
System.Threading.Thread.Sleep(1000);
}
#endregion
#region "Properties"
private static Shell32.Folder ControlPanelFolder
{
get
{
Shell32.Shell shell = new Shell32.Shell();
return shell.NameSpace(3);
}
}
private static Shell32.Folder NetworkFolder
{
get
{
Shell32.Folder retVal = null;
foreach (Shell32.FolderItem fi in ControlPanelFolder.Items())
{
//WindowsXP及以上需将下面的字符串改为:“网络连接”,XP以下为"网络和拨号连接"
if (fi.Name == "网络连接")
{
retVal=(Shell32.Folder)fi.GetFolder;
}
}
if (retVal == null)
{
throw (new NetworkConnectionsFolderNotFoundException());
}
else
{
return retVal;
}
}
}
private static Shell32.FolderItem LocalAreaConnectionFolderItem
{
get
{
Shell32.FolderItem retVal = null;
Properties.Settings set = Properties.Settings.Default;
foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())
{
Console.WriteLine(folderItem.Name);
if (folderItem.Name ==set.networkname)
{
retVal = folderItem;
goto endOfForLoop;
}
}
endOfForLoop:
if (retVal == null)
{
throw (new LocalAreaConnectionFolderItemNotFoundException());
}
else
{
return retVal;
}
}
}
private static Shell32.FolderItem WirelessConnectionFolderItem
{
get
{
Shell32.FolderItem retVal = null;
Properties.Settings set = Properties.Settings.Default;
foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())
{
Console.WriteLine(folderItem.Name);
if (folderItem.Name == set.networkname)
{
retVal = folderItem;
goto endOfForLoop;
}
}
endOfForLoop:
if (retVal == null)
{
throw (new WirelessConnectionFolderItemNotFoundException());
}
else
{
return retVal;
}
}
}
#endregion
#region "Custom Exceptions"
public class NetworkConnectionsFolderNotFoundException : Exception
{
public override string Message
{
get
{
return "The Network Connections Folder Could Not Be Found!";
}
}
}
public class LocalAreaConnectionFolderItemNotFoundException : Exception
{
public override string Message
{
get
{
return "The Local Area Connection Folder Could Not Be Found!";
}
}
}
public class WirelessConnectionFolderItemNotFoundException : Exception
{
public override string Message
{
get
{
return "The Wireless Connection Folder Could Not Be Found!";
}
}
}
#endregion
}
}
这篇文章算是抛砖引玉吧,因为里面用到的模拟按键的功能感觉挺实用的。。。
放上软件下载链接!