togglenetworkconnection.cs

来自「WindowsCE.Net程序设计-EVC程序源代码」· CS 代码 · 共 202 行

CS
202
字号
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())
			{
                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及以上需将下面的字符串改为:“网络连接”
                    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
		
	}
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?