winrom 实现在动配置IIS站点及程序池

参考文档:
                    http://www.cnblogs.com/cxd4321/p/4192407.html
                    http://blog.csdn.net/a497785609/article/details/27802685
需导入的命名空间:
                     using System.Security.AccessControl;
                     using Microsoft.Web.Administration;
                     using System.DirectoryServices; 
                     using System.Diagnostics;
                     using System.Management; 


     #region 获取本地IIS版本
        public static void GetIIsVersion()
        {
            try
            {
                DirectoryEntry entry = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
                string version = "本地IIS版本为:IIS" + entry.Properties["MajorIISVersionNumber"].Value.ToString();
                Console.WriteLine(version);
            }
            catch (Exception se)
            {
                Console.WriteLine(se);
            }
        }
        #endregion


     #region 创建应用程序池
        /// <summary>
        /// 创建引用程序池
        /// </summary>
        public static void CreateNewWebSite()
        {
            DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
            int Version = int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());
            if (Version > 6)
            {
                string AppPoolName = "LabManager";
                //检测是否存在
                if (!IsAppPoolName(AppPoolName))
                {
                    DirectoryEntry newpool;
                    DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                    newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
                    newpool.CommitChanges();
                }
                #region 修改应用程序的配置(包含托管模式及其NET运行版本)
                ServerManager sm = new ServerManager();
                sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
                sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated为集成 Classic为经典
                sm.CommitChanges();
                #endregion
            }
        }


        #region MyRegion 检测应用程序池是否存在
        private static bool IsAppPoolName(string AppPoolName)
        {
            bool result = false;
            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            //for循环寻找应用程序池下的所有子节点
            foreach (DirectoryEntry getdir in appPools.Children)
            {
                if (getdir.Name.Equals(AppPoolName))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        #endregion
       #endregion 

        #region 创建父站点
        public static void CreateFatherWeb()
        {
            DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry getentity in getdir.Children)
            {
                if (getentity.SchemaClassName.Equals("IIsWebServer"))
                {
                    foreach (DirectoryEntry getchild in getentity.Children)
                    {
                        if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
                        {
                            foreach (DirectoryEntry getsite in getchild.Children)
                            {

                                if (getsite.Name.Equals("bbbb"))
                                {
                                    getsite.DeleteTree();
                                    Console.WriteLine("应用程序已存在!");
                                    Console.WriteLine("执行删除操作!");
                                    Console.WriteLine("删除成功!");
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            string constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT";
            DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
            DirectoryEntry tbEntry = root.Children.Add("bbbb", "IIsWebVirtualDir");
            tbEntry.Properties["Path"][0] = @"C:\inetpub\wwwroot\bbbb";  //虚拟目录物理路径
            tbEntry.Invoke("AppCreate", true);//删除tbEntry.Invoke("AppDelete",true);   tbEntry.CommitChanges();     
            Console.WriteLine("创建成功!");
            //给父站点指定应用程序池名
            //  SetAppToPool("父站点名","Classic .NET AppPool");
        }

        #endregion 


    #region 创建子站点
        public static void CreatChildNewWeb()
        {
            Boolean IsExits = false;
            DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry getentity in getdir.Children)
            {
                if (getentity.SchemaClassName.Equals("IIsWebServer"))
                {
                    foreach (DirectoryEntry getchild in getentity.Children)
                    {
                        if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
                        {
                            foreach (DirectoryEntry getsite in getchild.Children)
                            {
                                //判断是否存在父站点
                                if (getsite.Name.Equals("ajaxtest"))
                                {
                                    IsExits = true;
                                    //循环父站点
                                    foreach (DirectoryEntry child in getsite.Children)
                                    {
                                        //判断父站点中是否存在子站点
                                        if (child.Name.Equals("aaaaaaa"))
                                        {
                                            Console.WriteLine("存在该子站点!");
                                            Console.WriteLine("执行删除操作!");
                                            child.DeleteTree();
                                            Console.WriteLine("删除成功!");
                                            break;
                                        }
                                    }
                                    //路径格式:C:\inetpub\wwwroot + 站点名 + WebFrame
                                    var url = @"C:\inetpub\wwwroot\ajaxtest\aaaaaaa";
                                    string constIISWebSiteRoot = "IIS://localhost/W3SVC/1/ROOT/ajaxtest";
                                    DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
                                    DirectoryEntry tbEntry = root.Children.Add("aaaaaaa", "IIsWebVirtualDir");
                                    tbEntry.Properties["Path"][0] = url;
                                    tbEntry.Invoke("AppCreate", true);
                                    Console.WriteLine("子站点创建成功!");
                                    //给子站点指定应用程序池名
                                    //  SetAppToPool("子站点名","ASP.NET v4.0 Classic");
                                }
                            }
                        }
                    }
                }
            }
            if (IsExits == false)
            {
                Console.WriteLine("不存在父站点,请先建立父站点");
            }
        } 
        #endregion




   #region 给站点设置应用程序池

        /// <summary>
        /// 给父站点指定应用程序池
        /// </summary>
        /// <param name="appname">站点名</param>
        /// <param name="poolName">应用程序池名</param>

       public static void SetAppToPool(string appname, string poolName)
       {
           //获取目录
           DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
           foreach (DirectoryEntry getentity in getdir.Children)
           {
               if (getentity.SchemaClassName.Equals("IIsWebServer"))
               {
                   //设置应用程序程序池 先获得应用程序 在设定应用程序程序池
                   //第一次测试根目录
                   foreach (DirectoryEntry getchild in getentity.Children)
                   {
                       if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
                       {
                           //找到指定的虚拟目录.
                           foreach (DirectoryEntry getsite in getchild.Children)
                           {
                               if (getsite.Name.Equals(appname))
                               {
                                   //【测试成功通过】
                                   getsite.Properties["AppPoolId"].Value = poolName;
                                   getsite.CommitChanges();
                                   Console.WriteLine("父站点应用程序池设置成功!");
                               }
                           }
                       }
                   }
               }
           }
       }
       

        public static void SetChildAppToPool(string appname, string poolName)
        {
            //获取目录
            DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");
            foreach (DirectoryEntry getentity in getdir.Children)
            {
                if (getentity.SchemaClassName.Equals("IIsWebServer"))
                {
                    //设置应用程序程序池 先获得应用程序 在设定应用程序程序池
                    //第一次测试根目录
                    foreach (DirectoryEntry getchild in getentity.Children)
                    {
                        if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))
                        {
                            //找到指定的虚拟目录.
                            foreach (DirectoryEntry getsite in getchild.Children)
                            {
                                if (getsite.Name.Equals("ajaxtest"))
                                {

                                    foreach (DirectoryEntry child in getsite.Children)
                                    {
                                        //判断父站点中是否存在子站点
                                        if (child.Name.Equals("aaaaaaa"))
                                        {
                                            //【测试成功通过】
                                            getsite.Properties["AppPoolId"].Value = poolName;
                                            getsite.CommitChanges();
                                            Console.WriteLine("子站点应用程序池设置成功!");
                                        }
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纪寻川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值