[天天解決一問題系列 - 0012] 如何經過程序獲取IIS站點信息

問題描述:操作系統

在WiX中須要判斷某個站點是否存在,WiX沒有這個能力,該怎麼作呢?.net

解決方案:blog

解決方法就是寫一個Custom Action來檢測,實現的途徑也有不少,如今想到了這麼幾個it

1)PowerShell 須要考慮操做系統的兼容性,只有Vista以上的操做系統才支持io

2)WMIclass

3).net 提供的庫以及IIS本身的庫, 注意須要區分版本兼容性

IIS 6 的代碼(需引用System.DirectoryServices.dll)foreach

       DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
            IEnumerator ie = Services.Children.GetEnumerator();
            DirectoryEntry Server = null;

            while (ie.MoveNext())
            {
                Server = (DirectoryEntry)ie.Current;
                if (Server.SchemaClassName == "IIsWebServer")
                {
                    Console.WriteLine(Server.Properties["ServerComment"][0].ToString());
                }
            }

IIS 七、8 的代碼 (需引用c:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll)引用

            var iisManager = new ServerManager();
            SiteCollection sites = iisManager.Sites;
            foreach (var s in sites)
            {
                Console.WriteLine(s.Name);
            }        
相關文章
相關標籤/搜索