shell32.dll 控制网络

        //禁用 SetNetworkAdapter(False) 
        //启用 SetNetworkAdapter(True) 
        //添加引用system32/shell32.dll 
        private static bool SetNetworkAdapter(bool status) 
        { 
            const string discVerb = "停用(&B)"; // "停用(&B)"; 
            const string connVerb = "启用(&A)"; // "启用(&A)"; 
            const string network = "网络连接"; //"网络连接"; 
            const string networkConnection = "VMware Network Adapter VMnet1"; // "本地连接" 

            string sVerb = null; 

            if (status) 
            { 
                sVerb = connVerb; 
            } 
            else 
            { 
                sVerb = discVerb; 
            } 

            Shell32.Shell sh = new Shell32.Shell(); 
            Shell32.Folder folder = sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfCONTROLS); 

            try 
            { 
                //进入控制面板的所有选项 
                foreach (Shell32.FolderItem myItem in folder.Items()) 
                { 
                    //进入网络连接 
                    if (myItem.Name == network) 
                    { 
                        Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder; 
                        foreach (Shell32.FolderItem fi in fd.Items()) 
                        { 
                            //找到本地连接 
                            if ((fi.Name == networkConnection)) 
                            { 
                                //找本地连接的所有右键功能菜单 
                                foreach (Shell32.FolderItemVerb Fib in fi.Verbs()) 
                                { 
                                    if (Fib.Name == sVerb) 
                                    { 
                                        Fib.DoIt(); 
                                        return true; 
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.Message); 
                return false; 
            } 
            return true; 
        } 

第一种出处:http://blog.csdn.net/leon_ice/article/details/4426278

再来一种方式 其实大体上是一样的 只不过第二种个人觉得比较赞 win7也适用

          /// <summary>
          /// 启用网络
          /// </summary>
          /// <param name="sender"></param>
          /// <param name="e"></param>
          private void btn_StartNet_Click(object sender, EventArgs e)
          {
               //SetNetworkAdapter(true);

               SetTest("本地连接", "启用"); // SetTest("本地连接", "禁用");
          }


          private void SetTest(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;
                         }
                    }
               }
          }

 

posted on 2015-09-24 00:23  无觉-李敏  阅读(355)  评论(0编辑  收藏  举报