我已经创建了一种从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)
中运行它时,它无效。它会给出以下错误< / p>
检索具有CLSID {2B&gt;的组件的COM类工厂 52-803546CE3344}由于以下错误而失败:80040154&gt; d(HRESULT异常:0x80040154(REGDB_E_CLASSNOTREG))。
任何帮助都会很充实?
答案 0 :(得分:2)
我使用了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;
}
}
答案 1 :(得分:1)
查看此博文,最具体地说,是最后一段。它很可能是32位与64位DLL编译冲突
我和客户都在创建32位.NET应用程序 FTP运行时状态的COM接口在a中实现 64位唯一的DLL。一旦我们都改变了我们的项目来编译 在64位平台上,我们都能够运行代码。 (巧合的是,当我写作时,我所拥有的只是一个32位系统 原创博客,所以如果我有的话,我可能会更快地遇到这个问题 当时拥有64位系统。 ; - ])