![](/img/trans.png)
[英]How do you list all the web sites (not applications) being hosted by an IIS server?
[英]To get the hosted web sites names from IIS
我創建了一種從IIS服務器獲取托管網站的方法,如下面的代碼片段所示。
ServerManager serverManager = new ServerManager();
try
{
foreach (Site site in serverManager.Sites)
{
Console.WriteLine(site);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
當我在我的本地機器上運行它時它運行得很好(Windows 7 /IIS 7 with 32bits)
當我在服務器機器(Windows server 2003 R2 with IIS 6)
運行它時,它無法工作。它給出了以下錯誤
由於以下錯誤,檢索具有CLSID {2B> 52-803546CE3344}的組件的COM類工廠失敗:80040154> d(來自HRESULT的異常:0x80040154(REGDB_E_CLASSNOTREG))。
任何幫助都會很棒嗎?
我使用System.DirectoryServices
而不是使用Microsoft.Web.Administration
它解決了我的問題。它也將與IIS6和IIS7一起使用。
public class IisManager
{
public static int IisVersion
{
get
{
int iisVersion;
using (RegistryKey iisKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\InetStp"))
{
if (iisKey == null)
throw new Exception("IIS is not installed.");
iisVersion = (int)iisKey.GetValue("MajorVersion");
}
return iisVersion;
}
}
public static IList<IisWebSite> GetIisSites()
{
List<IisWebSite> sites = new List<IisWebSite>();
using (DirectoryEntry iisRoot = new DirectoryEntry("IIS://localhost/W3SVC"))
{
iisRoot.RefreshCache();
sites.AddRange(iisRoot.Children.Cast<DirectoryEntry>().Where(w => w.SchemaClassName.ToLower(CultureInfo.InvariantCulture) == "iiswebserver").Select(w => new IisWebSite(w.Name, w.Properties["ServerComment"].Value.ToString())));
}
return sites;
}
public static IList<string> GetIisAppPools()
{
List<string> pools = new List<string>();
using (DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools"))
{
poolRoot.RefreshCache(); pools.AddRange(poolRoot.Children.Cast<DirectoryEntry>().Select(p => p.Name));
}
return pools;
}
}
查看此博客文章,最具體地說,是最后一段。 它很可能是32位與64位DLL編譯沖突
客戶和我都在創建32位.NET應用程序,而FTP運行時狀態的COM接口是在64位的DLL中實現的。 一旦我們都改變了我們的項目以編譯為64位平台,我們都能夠運行代碼。 (巧合的是,當我編寫原始博客時,我所擁有的只是一個32位系統,所以如果我當時擁有64位系統,我可能會更快地遇到這種情況。; - ])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.