如何使用c#在ASPX页面中获取IIS中的网站列表?

Jus*_*808 5 c# asp.net iis

我正在尝试使用

    DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
    foreach (DirectoryEntry de in root.Children)
    {
    }
Run Code Online (Sandbox Code Playgroud)

但我一直在

[COMException (0x80005000): Unknown error (0x80005000)]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +557
   System.DirectoryServices.DirectoryEntry.Bind() +44
   System.DirectoryServices.DirectoryEntry.get_IsContainer() +42
   System.DirectoryServices.ChildEnumerator..ctor(DirectoryEntry container) +36
   System.DirectoryServices.DirectoryEntries.GetEnumerator() +36
   IISVdir.List(String RootWeb) in c:\Development\Testing\App_Code\IISVdir.cs:116
   _Default.Page_Load(Object sender, EventArgs e) in c:\Development\Testing\Default.aspx.cs:11
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428
Run Code Online (Sandbox Code Playgroud)

JTe*_*Tew 0

我不太确定错误是什么,但猜测可能是安装问题或权限问题。

对于安装类型问题:

http://blogs.msdn.com/b/jpsanders/archive/2009/05/13/iis-7-adsi-error-system-runtime-interopservices-comexception-0x80005000-unknown-error-0x80005000.aspx

对于权限类型问题,在配置中添加一些内容,例如:

<identity impersonate="true" userName="AdminUserName" password="password" /> 
Run Code Online (Sandbox Code Playgroud)

或者将应用程序池运行所在的用户上下文更改为具有本地管理员权限的用户上下文也可以。

此外,IIS://localhost/W3SVC/1/Root 的子级将是虚拟目录或文件夹。网站将为 IIS://localhost/W3SVC。