【问题标题】:Is it possible that filter IIS worker processes with more criteria?是否可以使用更多条件过滤 IIS 工作进程?
【发布时间】:2020-04-22 07:54:08
【问题描述】:

我有一小段代码如下所示,用于列出所有 IIS 工作进程。 (w3wp.exe)

但是当存在多个w3wp.exe 进程时,我需要更多过滤条件。有应用程序池名称或站点名称的过滤选项吗?

var processes = ((DTE2)Marshal.GetActiveObject("VisualStudio.DTE.15.0"))
                              .Debugger
                              .LocalProcesses
                              .Cast<EnvDTE.Process>()                                      
                              .Where(proc => proc.Name.Contains("w3wp.exe"));

if (!processes.Any())
{
    Debug.WriteLine("no w3wp");
}
else if (processes.Count() > 1)
{
    Debug.WriteLine("multiple w3wp");      
    var p = processes.Where(x => ???).Single();
}
else
{
    Debug.WriteLine("single w3wp");
}

系统信息

  • IIS 10
  • Visual Studio 2017
  • .Net Framework 4.6.1

【问题讨论】:

标签: c# .net linq envdte visual-studio-automation


【解决方案1】:

我将我的解决方案分享给未来需要它的人。

找到您要查找的 ProcessId。

using System.Management;

public int GetProcessId()
{
  using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process where Name = 'w3wp.exe'"))
  {
    foreach (ManagementObject process in searcher.Get())
    {
      if (process["CommandLine"].ToString().Contains("**YOUR-SEARCH-CRITERIA**"))
      return Convert.ToInt32(process["ProcessId"].ToString());
    }
  }

 throw new Exception("Not found.");
}

比使用 ProcessId 附加。

var process = ((DTE2)Marshal.GetActiveObject("VisualStudio.DTE.15.0"))
                            .Debugger
                            .LocalProcesses
                            .Cast<EnvDTE.Process>()                                      
                            .Where(proc => proc.ProcessID == GetProcessId())
                            .FirstOrDefault();
if(process != null)
   process.Attach();

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多