I have a small block of code like below for listing all IIS worker process. (w3wp.exe)
But I need more filtering criteria when multiple w3wp.exe
processes exist. Is there any option for filter with Application Pool Name or Site Name?
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");
}
System Info
I shared my solution for those who will need it in the future.
Find the ProcessId you are looking for.
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.");
}
Than attach with the 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();