1. 程式人生 > >c#取得iis站點子站點和應用程式池的資訊

c#取得iis站點子站點和應用程式池的資訊

  /// <summary>
        /// 獲取當前IIS站點列表
        /// </summary>
        /// <returns></returns>
        public static List<IISStationInfo> GetLocalIISStations()
        {
            List<IISStationInfo> re = new List<IISStationInfo>();
            string entPath = "IIS://localhost/w3svc";
            DirectoryEntry ent = new DirectoryEntry(entPath);
            foreach (DirectoryEntry child in ent.Children)
            {
                if (child.SchemaClassName == "IIsWebServer")
                {


                    IISStationInfo item = new IISStationInfo();
                    item.ID = child.Name;
                    item.Name = child.Properties["ServerComment"].Value.ToString();
                    DirectoryEntry root = new DirectoryEntry(entPath + "/" + item.ID + "/ROOT");
                    item.Path = root.Properties["Path"].Value.ToString();
                    item.AppList = new List<IISAppInfo>(16);
                    foreach (DirectoryEntry app in root.Children)
                    {
                        if (app.SchemaClassName == "IIsWebVirtualDir")
                        {
                            IISAppInfo appitem = new IISAppInfo();
                            appitem.Name = app.Name;
                            appitem.ID = app.NativeGuid;
                            appitem.Path = app.Properties["Path"].Value.ToString();
                            appitem.AppName = app.Properties["AppPoolId"].Value.ToString();
                            item.AppList.Add(appitem);
                        }
                    }


                    re.Add(item);


                }
            }
            return re;

        }

 /// <summary>
    /// IIS站點資料封裝
    /// </summary>
    public class IISStationInfo
    {
        /// <summary>
        /// 站點名
        /// </summary>
        public string Name { get; set; }


        /// <summary>
        /// 站點指定路徑
        /// </summary>
        public string Path { get; set; }


        /// <summary>
        /// 站點識別符號
        /// </summary>
        public string ID { get; set; }


        /// <summary>
        /// 站點包含的應用程式,虛擬路徑等
        /// </summary>
        public List<IISAppInfo> AppList { get; set; }


    }


    /// <summary>
    /// IIS應用程式子站點資料封裝
    /// </summary>
    public class IISAppInfo
    {


        /// <summary>
        /// 站點
        /// </summary>
        public string Name { get; set; }


        /// <summary>
        /// 站點的制定路徑
        /// </summary>
        public string Path { get; set; }


        /// <summary>
        /// 站點的站點識別符號
        /// </summary>
        public string ID { get; set; }


        /// <summary>
        /// 站點的應用程式名稱
        /// </summary>
        public string AppName { get; set; }
    }