How can i get a list of the websites in IIS from within a ASPX page using c#?

5

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
c#
asp.net
iis
asked on Stack Overflow Mar 24, 2011 by Justin808

2 Answers

3

In Windows 7/8 go Control Panel / Program And Features / Turn Windows features on or off, and check all items from: Web Managment Tools, (it's include: IIS Managment Service, II 6 Managment Compatibility).

And you can use the code:

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); 
} 
answered on Stack Overflow Jun 26, 2014 by Hernaldo Gonzalez • edited Jun 26, 2014 by Hernaldo Gonzalez
0

I'm not too sure what the error is but at a guess it might be installation issue or permissions.

For installation type issues:

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

For permissions type issues adding something to config like:

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

Or changing the user context that the application pool runs under to one that has local admin rights could work.

Also, the children of IIS://localhost/W3SVC/1/Root will be Virtual Directories or Folders. Websites would be IIS://localhost/W3SVC.

answered on Stack Overflow Mar 24, 2011 by JTew

User contributions licensed under CC BY-SA 3.0