启用/禁用网卡网络连接 (C# 2005)

    今天朋友让我帮他做一个自动禁用/启用网络连接的小软件,因为他的电驴下载东西的时候,网络总断。解决原理可用PING命令不停PING某一网站,当不通时则启用/禁用网络连接。

但是让人郁闷的是,找了一天竟然没发现一个可以让网卡开启和关闭的代码。通过netsh也无法达到效果,XP下使用netsh根本就不能用。郁闷之极。。测试了几段代码,都无效。

    打算放弃的时候,发现了一段小程序,使用的是模拟鼠标按键的原理,打开控制面板,获取面板里的列表,找到网络连接并打开,再到想要关闭的相应项中模拟右键和左键。。很多都是硬编码,但是却实现了这个功能!~~

代码如下:

 

 

ContractedBlock.gif ExpandedBlockStart.gif 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
        
    }
    
}

 

这篇文章算是抛砖引玉吧,因为里面用到的模拟按键的功能感觉挺实用的。。。

放上软件下载链接!

http://files.cnblogs.com/mgod/ToggleNetWork_Csharp.rar

转载于:https://www.cnblogs.com/mgod/archive/2009/04/13/1434871.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值