How can I get a list of websites in IIS on an ASPX page using C #?

I'm trying to use

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

but i keep getting

[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
+5
source share
2 answers

I'm not too sure what this error is, but on the condition that it could be an installation or permission issue.

For problems with the type of installation:

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

For permission type types, adding something to the config is like:

<identity impersonate="true" userName="AdminUserName" password="password" /> 

Or, the user context that the application pool runs under one that has local administrator rights may change.

, IIS://localhost/W3SVC/1/Root . - IIS://localhost/W3SVC.

0

Windows 7/8 / / Windows : -, ( : IIS, Microsoft II 6).

:

public static void OpenWebsite(string name)
{
    DirectoryEntry Services = new DirectoryEntry("IIS://localhost/W3SVC");
    IEnumerator ie = Services.Children.GetEnumerator();
    DirectoryEntry Server = null;
    string nombre = "";

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

    Console.Write(nombre); 
} 
+2

All Articles