2009-02-04 75 views
3

如何爲特定網站獲取應用程序池名稱IIS 6的方案使用C#如何以編程方式獲取特定網站IIS6的應用程序池名稱? C#

編輯: 我已經使用的DirectoryServices命名空間,但應用程序池名稱的方法,除非它被明確設置不正確檢索使用相同的代碼。這意味着如果您使用iis管理器手動添加網站並設置應用程序池,那麼當我使用sharepoint創建應用程序並設置不同的appPool這些方法不工作時,這些代碼將無法工作(它將始終返回DefaultAppPool)。

回答

1

System.DirectoryServices namespace中的課程將幫助您獲取該信息。

檢查this article by Rick Strahl爲例:

/// <summary> 
/// Returns a list of all the Application Pools configured 
/// </summary> 
/// <returns></returns> 
public ApplicationPool[] GetApplicationPools() 
{   
    if (ServerType != WebServerTypes.IIS6 && ServerType != WebServerTypes.IIS7) 
     return null; 

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools"); 
     if (root == null) 
      return null; 

    List<ApplicationPool> Pools = new List<ApplicationPool>(); 

    foreach (DirectoryEntry Entry in root.Children) 
    { 
     PropertyCollection Properties = Entry.Properties; 

     ApplicationPool Pool = new ApplicationPool(); 
     Pool.Name = Entry.Name; 

     Pools.Add(Pool); 
    } 

    return Pools.ToArray(); 
} 

/// <summary> 
/// Create a new Application Pool and return an instance of the entry 
/// </summary> 
/// <param name="AppPoolName"></param> 
/// <returns></returns> 
public DirectoryEntry CreateApplicationPool(string AppPoolName) 
{ 
    if (this.ServerType != WebServerTypes.IIS6 && this.ServerType != WebServerTypes.IIS7) 
     return null; 

    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools"); 
    if (root == null) 
     return null; 

    DirectoryEntry AppPool = root.Invoke("Create", "IIsApplicationPool", AppPoolName) as DirectoryEntry;   
    AppPool.CommitChanges(); 

    return AppPool; 
} 

/// <summary> 
/// Returns an instance of an Application Pool 
/// </summary> 
/// <param name="AppPoolName"></param> 
/// <returns></returns> 
public DirectoryEntry GetApplicationPool(string AppPoolName) 
{ 
    DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName); 
    return root; 
} 

/// <summary> 
/// Retrieves an Adsi Node by its path. Abstracted for error handling 
/// </summary> 
/// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param> 
/// <returns>node or null</returns> 
private DirectoryEntry GetDirectoryEntry(string Path) 
{ 

    DirectoryEntry root = null; 
    try 
    { 
     root = new DirectoryEntry(Path); 
    } 
    catch 
    { 
     this.SetError("Couldn't access node"); 
     return null; 
    } 
    if (root == null) 
    { 
     this.SetError("Couldn't access node"); 
     return null; 
    } 
    return root; 
} 
+0

我已經使用這些方法,但應用程序池名稱不能正確檢索,除非通過使用相同的代碼明確設置。 這意味着如果您使用iis管理器手動添加網站並設置應用程序池,那麼當我使用sharepoint創建應用程序並設置不同的appPool時,這些代碼將無法工作(它總是會返回DefaultAppPool) – 2009-02-04 13:30:21

+0

dont工作。 – 2009-02-04 13:30:58

1

總之,有這樣做的春天在腦海中的2種方式。

的不太成熟的方法是知道,IIS6的設置存儲在其中只是一個XML文件的元數據庫:

C:\WINDOWS\system32\inetsrv\MetaBase.xml 

你可以只使用Linq2Xml和解析XML尋找網站名稱或ID,該AppPoolId屬性包含的應用程序池

正確的方法名是使用的System.DirectoryServices

7

我不同意你的看法。我編寫了一個測試應用程序,並從中得到正確的AppPool名稱,即使我使用IIS管理器手動設置AppPool。

爲了確定,我測試過一次,名字沒問題;然後,我打開IIS管理器,更改AppPool,執行iisreset,並再次運行測試應用程序 - 我得到的AppPool名稱再次正確。我不知道你的代碼是怎麼樣的,但我的是這樣的:

using System; 
using System.IO; 
using System.DirectoryServices; 

class Class 
{ 
    static void Main(string[] args) 
    { 
     DirectoryEntry entry = FindVirtualDirectory("<Server>", "Default Web Site", "<WantedVirtualDir>"); 
     if (entry != null) 
     { 
      Console.WriteLine(entry.Properties["AppPoolId"].Value); 
     } 
    } 

    static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir) 
    { 
     DirectoryEntry siteEntry = null; 
     DirectoryEntry rootEntry = null; 
     try 
     { 
      siteEntry = FindWebSite(server, website); 
      if (siteEntry == null) 
      { 
       return null; 
      } 

      rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir"); 
      if (rootEntry == null) 
      { 
       return null; 

      } 

      return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir"); 
     } 
     catch (DirectoryNotFoundException ex) 
     { 
      return null; 
     } 
     finally 
     { 
      if (siteEntry != null) siteEntry.Dispose(); 
      if (rootEntry != null) rootEntry.Dispose(); 
     } 
    } 

    static DirectoryEntry FindWebSite(string server, string friendlyName) 
    { 
     string path = String.Format("IIS://{0}/W3SVC", server); 

     using (DirectoryEntry w3svc = new DirectoryEntry(path)) 
     { 
      foreach (DirectoryEntry entry in w3svc.Children) 
      { 
       if (entry.SchemaClassName == "IIsWebServer" && 
        entry.Properties["ServerComment"].Value.Equals(friendlyName)) 
       { 
        return entry; 
       } 
      } 
     } 
     return null; 
    } 
} 

對不起,我糟糕的英語。
希望我幫了忙。

相關問題