2010-01-29 13 views
4

我的服務器上運行了幾個網站。確定給定應用程序池的w3wp System.Diagnostics.Process

我有一個「診斷」頁面的應用程序顯示內存量爲當前進程的內存(非常有用)。

現在這個應用程序與另一個應用程序「連接」,並且我希望我的診斷頁面能夠爲另一個w3wp進程顯示大量內存。

要獲得的內存量,我用一個簡單的代碼:

var process = Process.GetProcessesByName("w3wp"); 
string memory = this.ToMemoryString(process.WorkingSet64); 

我如何識別我的第二個w3wp進程,知道它的應用程序池?

我發現了一個相應的線程,但沒有合適的答案: Reliable way to see process-specific perf statistics on an IIS6 app pool

感謝

回答

6

你可以使用WMI,以確定哪個應用程序池指定w3wp.exe過程屬於:

var scope = new ManagementScope(@"\\YOURSERVER\root\cimv2"); 
var query = new SelectQuery("SELECT * FROM Win32_Process where Name = 'w3wp.exe'"); 
using (var searcher = new ManagementObjectSearcher(scope, query)) 
{ 
    foreach (ManagementObject process in searcher.Get()) 
    { 
     var commandLine = process["CommandLine"].ToString(); 
     var pid = process["ProcessId"].ToString(); 
     // This will print the command line which will look something like that: 
     // c:\windows\system32\inetsrv\w3wp.exe -a \\.\pipe\iisipm49f1522c-f73a-4375-9236-0d63fb4ecfce -t 20 -ap "NAME_OF_THE_APP_POOL" 
     Console.WriteLine("{0} - {1}", pid, commandLine); 
    } 
} 
5

你也可以使用IIS ServerManager組件獲取PID;方式,如果你需要在代碼中訪問它,而不重定向和解析控制檯輸出;

public static int GetIISProcessID(string appPoolName) 
{ 
    Microsoft.Web.Administration.ServerManager serverManager = new 
     Microsoft.Web.Administration.ServerManager(); 
    foreach (WorkerProcess workerProcess in serverManager.WorkerProcesses) 
    { 
     if (workerProcess.AppPoolName.Equals(appPoolName)) 
      return workerProcess.ProcessId; 
    } 

    return 0; 
} 
+0

就是這樣! – Drutten 2016-09-29 10:05:39