C#实现启用或停用本地网络连接,转
2009-05-15 14:58
369 查看
http://blog.csdn.net/ojlovecd/archive/2009/02/28/3945605.aspx
首先添加Microsoft Shell Control And Automation引用,
using System;
using System.Threading;
class test
{
static void Main(string[] args)
{
NetWork("本地连接", "启用");
}
/// <summary>
/// 实现启用或停用本地网络链接
/// </summary>
/// <param name="netWorkName">本地连接名称</param>
/// <param name="operation">操作,传入“启用”或“停用”</param>
static void NetWork(string netWorkName, string operation)
{
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(49);
foreach (Shell32.FolderItem fi in folder.Items())
{
if (fi.Name != netWorkName)
continue;
Shell32.ShellFolderItem folderItem = (Shell32.ShellFolderItem)fi;
foreach (Shell32.FolderItemVerb fiv in folderItem.Verbs())
{
if (!fiv.Name.Contains(operation))
continue;
else
{
fiv.DoIt();
Thread.Sleep(1000);
break;
}
}
}
}
}
代码说明:
NetWork方法的两个参数说明:第一个为要进行操作的本地连接名称,第二个为你要进行的操作,传入“启用”或“停用”(win2000下可能是“禁用”)
这个方法的原理是先找到“网络连接”这个虚拟文件夹,其中shell.NameSpace(49)中的49据说是为了避免遍历控制面板,我没有测试过,有兴趣的朋友可以试试。然后找到要控制的本地连接对应的folderitem,然后枚举verb,找到需要的verb后,调用verb的DoIt方法,在DoIt的时候加了一个Thread.Sleep(1000);是为了使程序不会由于遍历的太快而使操作失效,上述方法已测试通过,如有不足之处请指出,参考资料:
http://www.cnblogs.com/rainstormmaster/archive/2006/02/11/328943.html
首先添加Microsoft Shell Control And Automation引用,
using System;
using System.Threading;
class test
{
static void Main(string[] args)
{
NetWork("本地连接", "启用");
}
/// <summary>
/// 实现启用或停用本地网络链接
/// </summary>
/// <param name="netWorkName">本地连接名称</param>
/// <param name="operation">操作,传入“启用”或“停用”</param>
static void NetWork(string netWorkName, string operation)
{
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(49);
foreach (Shell32.FolderItem fi in folder.Items())
{
if (fi.Name != netWorkName)
continue;
Shell32.ShellFolderItem folderItem = (Shell32.ShellFolderItem)fi;
foreach (Shell32.FolderItemVerb fiv in folderItem.Verbs())
{
if (!fiv.Name.Contains(operation))
continue;
else
{
fiv.DoIt();
Thread.Sleep(1000);
break;
}
}
}
}
}
代码说明:
NetWork方法的两个参数说明:第一个为要进行操作的本地连接名称,第二个为你要进行的操作,传入“启用”或“停用”(win2000下可能是“禁用”)
这个方法的原理是先找到“网络连接”这个虚拟文件夹,其中shell.NameSpace(49)中的49据说是为了避免遍历控制面板,我没有测试过,有兴趣的朋友可以试试。然后找到要控制的本地连接对应的folderitem,然后枚举verb,找到需要的verb后,调用verb的DoIt方法,在DoIt的时候加了一个Thread.Sleep(1000);是为了使程序不会由于遍历的太快而使操作失效,上述方法已测试通过,如有不足之处请指出,参考资料:
http://www.cnblogs.com/rainstormmaster/archive/2006/02/11/328943.html
相关文章推荐
- C#实现启用或停用本地网络连接
- C#实现启用与禁用本地网络的方式小结【3种方式】
- 用代码实现"停用/启用"网络连接
- C#实现启用、禁用本地网络的三种方式
- C#实现启用、禁用本地网络的三种方式
- C#实现启用、禁用本地网络的三种方式
- 【转】VBS脚本 实现"无线网络连接"与"本地连接"的网卡禁用启用完美切换(优化版)
- 无法使用VMware以NAT模式连接本地网络(实现虚拟机跟主机共享网络)
- [C#][共享网络] Netsh命令实现共享,并查询连接用户
- 用vbs实现本地连接禁用/启用脚本代码
- win7本地连接共享网络实现手机wifi访问
- 用Delphi实现关闭、重启本地网络连接
- 编程启用/禁用本地连接(C#2005)
- 编程启用/禁用本地连接(C#2005)
- 基于Actor模式的c#网络游戏服务器的实现和Unity游戏客户端的连接
- C#中判断本地系统的网络连接状态的方法
- 网络连接无法正常停用和启用的解决方法
- C#判断本地系统的网络连接状态
- Delphi实现WINDOWS“本地连接”的启用和禁用