C#读取IIS信息

2011-1-19 14:39:14 - tanlixiao

C#读取IIS信息,读取IIS的各站点信息,虚拟目录信息和物理路径信息;当一IIS网站绑定多个域名名称时,只读取第一个,

    public class IISWorker
    {
        private static string HostName= "localhost";
        private static string UserName="";
        private static string Password="";

        #region 根据路径构造Entry的方法
        /// 根据是否有用户名来判断是否是远程服务器。
        /// 然后再构造出不同的DirectoryEntry出来
        /// DirectoryEntry的路径
        /// 返回的是DirectoryEntry实例
        public static DirectoryEntry GetDirectoryEntry(string entPath)
        {
            DirectoryEntry ent;
            if (UserName == null)
            {
                ent = new DirectoryEntry(entPath);
            }
            else
            {
                ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
            }
            return ent;
        }
        #endregion

        #region 得到网站的物理路径
        /// <summary>
        /// 得到网站的物理路径
        /// </summary>
        /// <param name="rootEntry">网站节点</param>
        /// <returns></returns>
        public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)
        {
            string physicalPath = "";
            foreach (DirectoryEntry childEntry in rootEntry.Children)
            {
                if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))
                {
                    if (childEntry.Properties["Path"].Value != null)
                    {
                        physicalPath = childEntry.Properties["Path"].Value.ToString();
                    }
                    else
                    {
                        physicalPath = "";
                    }
                }
            }
            return physicalPath;
        }
        #endregion
        /// <summary>
        /// 获取站点名
        /// </summary>
        public static List<IISInfo> GetServerBindings()
        {
            List<IISInfo> iisList = new List<IISInfo>();
            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry ent = GetDirectoryEntry(entPath);
            foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
                {
                    if (child.Properties["ServerBindings"].Value != null)
                    {
                        object objectArr = child.Properties["ServerBindings"].Value;
                        string serverBindingStr = string.Empty;
                        if (IsArray(objectArr))//如果有多个绑定站点时
                        {
                            object[] objectToArr = (object[])objectArr;
                            serverBindingStr = objectToArr[0].ToString();
                        }
                        else//只有一个绑定站点
                        {
                            serverBindingStr = child.Properties["ServerBindings"].Value.ToString();
                        }
                        IISInfo iisInfo = new IISInfo();
                        iisInfo.DomainPort = CoverWebSitePost(serverBindingStr);
                        iisInfo.MonitorName = GetWebSiteFirstName(serverBindingStr);
                        monitorUrl selectMonitor = ValidateMonitorName(iisInfo.MonitorName);
                        if (selectMonitor!=null)
                        {
                            iisInfo.SearchKeyWord = selectMonitor.Searchkey;
                            iisInfo.Url = selectMonitor.Monitorurl;
                        }
                        iisList.Add(iisInfo);
                    }
                }
            }
            return iisList;
        }
        /// <summary>
        /// 获取虚拟目录
        /// </summary>
        public static List<IISInfo> GetVirtualDir()
        {
            Hashtable hashTVirtual = new Hashtable();
            List<IISInfo> iisList = new List<IISInfo>();
            string entPath = String.Format("IIS://{0}/w3svc", HostName);
            DirectoryEntry ent = GetDirectoryEntry(entPath);
            foreach (DirectoryEntry entry in ent.Children)
            {
                if (entry.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))
                {
                    DirectoryEntry virEntry = new DirectoryEntry(entry.Path "/ROOT");
                    foreach (DirectoryEntry entryVirtual in virEntry.Children)
                    {
                        IISInfo iisInfo = new IISInfo();
                        if (entryVirtual.SchemaClassName.Equals("IIsWebVirtualDir", StringComparison.OrdinalIgnoreCase))
                        {
                            //hashTVirtual.Add(entryVirtual.Name, entryVirtual.Properties["AppPoolId"].Value);
                            iisInfo.DomainPort = CoverWebSitePost(entry.Properties["ServerBindings"].Value.ToString());
                            iisInfo.MonitorName = CoverWebSitePost(entry.Properties["ServerBindings"].Value.ToString()).Split(':')[0];//entryVirtual.Name;
                            iisList.Add(iisInfo);
                        }
                    }
                }
            }
            return iisList;
        }
        /// <summary>
        /// 转化域名和端口
        /// </summary>
        public static string CoverWebSitePost(string serverBindings)
        {
            string sitePost = string.Empty;
            string[] serverBindArr = serverBindings.Split(':');
            if (serverBindArr != null && serverBindArr.Length > 2)
            {
                sitePost = serverBindArr[2] ":" serverBindArr[1];
            }
            return sitePost;
        }
        /// <summary>
        /// 取得域名和端口号
        /// </summary>
        public static string GetWebSiteFirstName(string serverBindings)
        {
            string siteFirst = string.Empty;
            string[] serverBindArr = serverBindings.Split(':');
            if (serverBindArr != null && serverBindArr.Length > 2)
            {
                siteFirst =serverBindArr[2];
            }
            return siteFirst;
        }
        /// <summary>
        /// 验证域名是否在配置文件的监控页中
        /// </summary>
        /// <param name="monitorName">域名</param>
        /// <returns></returns>
        public static monitorUrl ValidateMonitorName(string monitorName)
        {
            List<monitorUrl> listMonitorUrl = ReadXml.ReadMonitorUrl();
            monitorUrl selectMonitorUrl = null;
            foreach (monitorUrl item in listMonitorUrl)
            {
                if (WebClient.IsContain(item.Domain,monitorName))
                {
                    selectMonitorUrl = item;
                    break;
                }
            }
            return selectMonitorUrl;
        }
        /// <summary>
        /// 判断object对象是否为数组
        /// </summary>
        public static bool IsArray(object o)
        {
            return o is Array;
        }

    }

标签:C# IIS信息

关于我

喜欢简简单单的生活.

记录平凡世界的点滴

-于2010年12月31日

联系我

  • 微信:fishren
  • 邮箱:
    tanlixiao@163.com
  • Address:
    中国最南端的滨海城市
    河西区 解放路 502