如何以编程方式列出IIS工作进程?

时间:2015-11-24 05:56:26

标签: c# .net iis windows-7 wmi

我已尝试使用WorkerProcess类建议here,但收到“System.Management.ManagementException:Invalid class”错误消息。

        var scope = new ManagementScope("//./root/CIMV2");
        var searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM WorkerProcess"));

还有其他想法吗?

1 个答案:

答案 0 :(得分:0)

如果这对于IIS 7+而言您可以轻松使用ServerManager,您可以在此处找到一些示例:http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx

对于WorkerProcesses,您可以这样做:

using(ServerManager iisManager = new ServerManager()) {
  foreach(WorkerProcess w3wp in iisManager.WorkerProcesses) {
    Console.WriteLine("W3WP ({0})", w3wp.ProcessId);

    foreach (Request request in w3wp.GetRequests(0)) {
        Console.WriteLine("{0} - {1},{2},{3}",
                    request.Url,
                    request.ClientIPAddr,
                    request.TimeElapsed,
                    request.TimeInState);
    }
  }
}