首页蓝耳朵|小小蓝耳朵广州图书馆外文室英语儿童读物资源介绍网佛教青年之友旧版收集永硕E盘Phonics Short Vowels Game 
用DirectoryEntry读取AppPoolId一直返回DefaultAppPool的原因
所属栏目:winform  时间:2011-07-26 20:57  作者:狼子

我的理解是每个独立程序池的网站都有两个应用程序池的,第一个是网站的程序池,如果没有用代码指定,只是在IIS里添加或修改,是DefalutAppPool,第二个是网站下面虚拟目录用的程序池,是指定的那个

在IIS的网站属性里,主目录标签,应用程序池的下拉菜单里选择设置的应用程序池是虚拟目录的应用程序池,不是网站的,这个在C:\WINDOWS\system32\inetsrv\MetaBase.xml可以看出来,AppPoolId是定义在IIsWebVirtualDir节点里的

所以在读取AppPoolId时,要循环到虚拟目录

#region 取指定网站正在使用的应用程序池
        /// <summary>
        /// NNLL 20100726 1933 取指定网站正在使用的应用程序池
        /// </summary>
        /// <param name="siteName">指定网站的名称</param>
        /// <returns></returns>
        public static string GetAppPoolForSite(string siteName)
        {
            string poolName = null;
            using (DirectoryEntry root = new DirectoryEntry(webSitePath))
            {
                //取指定网站
                foreach (DirectoryEntry site in root.Children)
                {
                    if (site.SchemaClassName.Equals("IIsWebServer")
                        &&
                        site.Properties["ServerComment"].Value.ToString().Equals(siteName)
                        )
                    {
                        //取网站的应用程序池,如果是程序添加的,网站的应用程序池可以被手动指定
                        poolName = site.Properties["AppPoolId"].Value.ToString();
                        if (poolName.Equals("DefalutAppPool"))
                        {
                            //网站的应用程序池是默认的,取虚拟目录的应用程序池,如果是在IIS里修改过应用程序池,被修改的是虚拟目录的,从C:\WINDOWS\system32\inetsrv\MetaBase.xml的IIsWebVirtualDir节点可以看出
                            foreach (DirectoryEntry virtualDir in site.Children)
                            {
                                if (virtualDir.SchemaClassName.Equals("IIsWebVirtualDir"))
                                {
                                    poolName = virtualDir.Properties["AppPoolId"].Value.ToString();
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }
            }
            return poolName;
        }
        #endregion

下面两个方法是取指定标志符的网站的信息,这个在修改网站资料时会用到:

#region 根据网站标志符,获取指定网站的入口
        private static DirectoryEntry GetWebSiteEntry(string siteId)
        {
            DirectoryEntry webSite = null;
            try
            {
                using (DirectoryEntry root = new DirectoryEntry(webSitePath))
                {
                    webSite = root.Children.Find(siteId, "IIsWebServer");
                    if (webSite == null)
                    {
                        MessageBox.Show("指定网站不存在");
                        return null;
                    }
                }
            }
            catch (Exception ex)
            {
                webSite = null;
                MessageBox.Show("获取指定网站失败:" + ex.Message);
            }
            return webSite;
        }
        #endregion

#region 获取指定标签符的网站的信息
        /// <summary>
        /// 获取指定标签符的网站的信息
        /// </summary>
        /// <param name="siteId">指定网站的标志符</param>
        /// <param name="serverComment">网站描述</param>
        /// <param name="ip">IP地址</param>
        /// <param name="port">端口号</param>
        /// <param name="hostname">主机头</param>
        /// <param name="path">文件路径</param>
        /// <param name="defaultDoc">默认文档</param>
        /// <param name="netVersion">.net framework的版本</param>
        /// <param name="appPoolId">应用程序池的名称</param>
        /// <param name="enableDirBrowsing">浏览权限</param>
        /// <param name="accessRead">读取权限</param>
        /// <param name="accessExecute">执行(如ISAPI应用程序或CGI)权限</param>
        /// <param name="accessWrite">写入权限</param>
        /// <param name="accessScript">运行脚本(如ASP)权限</param>
        /// <returns></returns>
        public static bool GetWebSiteInfo(string siteId, out string serverComment,
            out string ip, out string port, out string hostname, out string path, out string defaultDoc,
            out string netVersion, out string appPoolId, out bool enableDirBrowsing,
            out bool accessRead, out bool accessExecute, out bool accessWrite, out bool accessScript)
        {
            bool ok = true;
            //初始化变量
            serverComment = ip = port = hostname = path = defaultDoc = netVersion = appPoolId = "";
            enableDirBrowsing = accessRead = accessExecute = accessWrite = accessScript = false;

            using (DirectoryEntry site = GetWebSiteEntry(siteId))
            {
                if (site == null) return false;

                try
                {
                    #region 取在网站定义的属性
                    serverComment = site.Properties["ServerComment"].Value.ToString();
                    string serverBindings = site.Properties["ServerBindings"].Value.ToString();
                    string[] serverBindingArray = serverBindings.Split(':');
                    ip = serverBindingArray[0];
                    port = serverBindingArray[1];
                    hostname = serverBindingArray[2];
                    defaultDoc = site.Properties["DefaultDoc"].Value.ToString();
                    appPoolId = site.Properties["AppPoolId"].Value.ToString();
                    #endregion

                    #region 取在虚拟目录定义的属性
                    using (DirectoryEntry virtualDir = site.Children.Find("Root", "IIsWebVirtualDir"))
                    {
                        if (virtualDir != null)
                        {
                            //应用程序池以根虚拟目录的为准
                            if (appPoolId.Equals("DefaultAppPool"))
                            {
                                appPoolId = virtualDir.Properties["AppPoolId"].Value.ToString();
                            }
                            path = virtualDir.Properties["Path"][0].ToString();
                            enableDirBrowsing = Convert.ToBoolean(virtualDir.Properties["EnableDirBrowsing"][0]);
                            accessRead = Convert.ToBoolean(virtualDir.Properties["AccessRead"][0]);
                            accessExecute = Convert.ToBoolean(virtualDir.Properties["AccessExecute"][0]);
                            accessWrite = Convert.ToBoolean(virtualDir.Properties["AccessWrite"][0]);
                            accessScript = Convert.ToBoolean(virtualDir.Properties["AccessScript"][0]);

                            #region 取.net framework的版本
                            object[] scriptMaps = (object[])virtualDir.Properties["ScriptMaps"].Value;
                            foreach (object scriptMap in scriptMaps)
                            {
                                if (scriptMap.ToString().Contains("v1.1.4322"))
                                {
                                    netVersion = "1.1.4322";
                                    break;
                                }
                                else if (scriptMap.ToString().Contains("v2.0.50727"))
                                {
                                    netVersion = "2.0.50727";
                                    break;
                                }
                                else if (scriptMap.ToString().Contains("v4.0.30319"))
                                {
                                    netVersion = "4.0.30319";
                                    break;
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    ok = false;
                    MessageBox.Show("读取网站资料失败:" + ex.Message);
                }
            }
            return ok;
        }
        #endregion

小楼宝宝的涂鸦花花(Imitater)的博客起名称骨测字