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

来源:互联网 发布:怎么给淘宝宝贝配图 编辑:程序博客网 时间:2024/05/16 04:36
参考文档:
                    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 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证和行驶证被交警扣了怎么办 行驶证被交警弄丢了怎么办 没带行驶证让警察发现怎么办 首尔转机换票换乘怎么办过境签证 驾驶证约考帐号与登密码丢失怎么办 考驾照的时候预约密码忘了怎么办 考驾照的预约密码忘了怎么办 摩托车不能挂档不能摘挡了怎么办 身份证丢失了派出所不给挂失怎么办 快递员在中午还送货夏天怎么办啊 驾照一个记分周期扣满12分怎么办 佛山南海车管所怎么办替人消分流程 福州快处中心几流程要怎么办 被对方追尾了对方只有交强险怎么办 摩托车行驶证年检过期一年半怎么办 户口迁到杭州了身份证掉了怎么办 户口已迁至外面要补办结婚证怎么办 汽车年检时间过了1个月怎么办 户口在老家在北京工作怎么办招工 报考驾照时手机号留错了怎么办 合肥驾照换证体检没过怎么办 驾驶证记分周期到了违章未消怎么办 驾照被扣科目一过期没考怎么办 驾驶证暂扣过了换证日期怎么办? 考驾照科目二身份证丢了怎么办 驾照科目一考试身份证丢了怎么办 换驾驶证名下有车辆脱审怎么办 交警把驾驶证和行车证扣了怎么办 连续两天驾照都是扣12分怎么办? 交警开的电动车罚单丢了怎么办 驾驶证b证体检报告拖期怎么办 在中国把美国护照弄丢了怎么办 拿了驾照两年了不敢上高速怎么办 我要移民过香港大陆的驾驶证怎么办 交警开的扣行驶证的单不见了怎么办 行驶证累计记分满12分后怎么办 行车证丢了被交警查到怎么办 手机摔了一下一半黑屏了怎么办 三星手机的显示屏插头坏了怎么办? 被普通的手机维修店骗了怎么办? 手机外屏裂了内屏进水了怎么办