2008-12-02 80 views
4

我需要知道如何檢測當前正在運行的應用程序池,因此我可以通過編程方式對其執行回收。如何檢測我當前正在運行的應用程序池? (IIS6)

有沒有人知道如何做到這一點的IIS6?

我回收的應用程序池當前的代碼:

/// <summary> 
    /// Recycle an application pool 
    /// </summary> 
    /// <param name="IIsApplicationPool"></param> 
    public static void RecycleAppPool(string IIsApplicationPool) { 
     ManagementScope scope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2"); 
     scope.Connect(); 
     ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/" + IIsApplicationPool + "'"), null); 

     appPool.InvokeMethod("Recycle", null, null); 
    } 

回答

7

而且搜索後,我發現自己的答案:

public string GetAppPoolName() { 

     string AppPath = Context.Request.ServerVariables["APPL_MD_PATH"]; 

     AppPath = AppPath.Replace("/LM/", "IIS://localhost/"); 
     DirectoryEntry root = new DirectoryEntry(AppPath); 
     if ((root == null)) { 
      return " no object got"; 
     } 
     string AppPoolId = (string)root.Properties["AppPoolId"].Value; 
     return AppPoolId; 
    } 

嗯。他們需要一種方法讓我把自己的答案設定爲答案。

+0

你現在可以做到這一點。 – 2009-01-21 09:09:35

2

我發現這一個以及它爲我工作。請注意,您可能需要包含using System.DirectoryServices的參考文獻;

private static string GetCurrentApplicationPoolId() 
    { 
     string virtualDirPath = AppDomain.CurrentDomain.FriendlyName; 
     virtualDirPath = virtualDirPath.Substring(4); 
     int index = virtualDirPath.Length + 1; 
     index = virtualDirPath.LastIndexOf("-", index - 1, index - 1); 
     index = virtualDirPath.LastIndexOf("-", index - 1, index - 1); 
     virtualDirPath = "IIS://localhost/" + virtualDirPath.Remove(index); 
     DirectoryEntry virtualDirEntry = new DirectoryEntry(virtualDirPath); 
     return virtualDirEntry.Properties["AppPoolId"].Value.ToString(); 
    } 
相關問題