2011-03-24 27 views
5

我試圖使用如何從ASPX頁面使用C#獲取IIS中的網站列表?

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

,但我不斷收到

[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 
+0

這大概重複了另一個常見的解釋 - http://stackoverflow.com/questions/3782058/using-directoryentry-to-enumerate-iis-configuration-數據獲取,收到COMException – 2012-10-09 14:07:50

回答

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" /> 

或改變用戶上下文應用程序池下運行,以一個有本地管理員權限可以工作。

此外,IIS:// localhost/W3SVC/1/Root的子項將成爲虛擬目錄或文件夾。網站將是IIS:// localhost/W3SVC。

2

在Windows 7/8去控制面板/程序和功能/啓用Windows功能打開或關閉,並檢查所有項目來自:網絡梟雄工具,(它的包括:IIS梟雄服務,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); 
} 
相關問題