winForm:管理网络连接开关的小程序

今天在调一IM断线重连的功能,一直拨插本本的网口,十几次下来后有点心疼。可又懒得每次切换过来切换过去 ,选中后点右健,更甚至有时不知不觉老是把网络连接那窗口关掉,每次都要点那么好几下才成功,一气之下找了段VB的例子,照着改改,能用用。嘿嘿

 

打开之后先选好某一网络连接,然后最小化到托盘,以后每次双击即是改变了。嘿嘿

 

 

 

双击默认是点击按钮,嘿嘿

这样就方便多了,也不用一直去拨插,心疼本本呀

 

代码很简单,也很无奈,里面有好几个地方硬编码(英文版,还有2K的具体情况再定喽),主要是用一个DLLSHELL32

这里给出原作者的地址,嘿嘿。照抄过来就是了

 

http://www.codeproject.com/KB/vb/toggleNetworkConn.aspx

 

获取系统控制面板文件夹

       private static Shell32.Folder ControlPanelFolder

        {

            get

            {

                Shell32.Shell shell = new Shell32.Shell();

                return shell.NameSpace(3);

            }

        }

 

获取控制面板下的网络连接(注这里的网络连接是XP下的,2K下的自已改)

        private static Shell32.Folder NetworkFolder

        {

            get

            {

                Shell32.Folder retVal = null;

 

                foreach (Shell32.FolderItem fi in ControlPanelFolder.Items())

                {

                   if (fi.Name == "网络连接")

                    {

                        retVal = (Shell32.Folder)fi.GetFolder;

                    }

                }

                if (retVal == null)

                {

                   throw (new NetworkConnectionsFolderNotFoundException());

                }

                else

                {

                    return retVal;

                }

            }

        }

 

取得几个网络连接的列表,一般现在都不止单网卡

        public static List<string> NetWorkFolderList()

        {

            List<string> LNetWorkFolder = new List<string>();

            foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())

            {

                LNetWorkFolder.Add(folderItem.Name);

            }

            return LNetWorkFolder;

        }

通过名称获取相应的图标

private static Shell32.FolderItem GetItem(string str)

        {

            Shell32.FolderItem retVal = null;

            foreach (Shell32.FolderItem folderItem in NetworkFolder.Items())

            {

                if (folderItem.Name == str)

                {

                    retVal = folderItem;

                    break;

                }

            }

 

执行右健功能

Shell32.FolderItem item = GetItem(str);

            foreach (Shell32.FolderItemVerb verb in item.Verbs())

            {

                if (verb.Name == "启用(&A)" || verb.Name == "停用(&B)")

                {

                    verb.DoIt();

                }

            }

 

            if (retVal == null)

            {

                throw (new LocalAreaConnectionFolderItemNotFoundException());

            }

            else

            {

                return retVal;

            }

        }

 

代码主要部分就在这里了喽,嘿嘿,有兴趣的朋友可以跟着做一个,嘿嘿。这个例子估计也是老WIN32程序员才做得出,改天找找类库里有没可以实现的,嘿嘿。

 

 

 

posted @ 2008-11-04 20:20  yellowyu  阅读(1385)  评论(0编辑  收藏  举报