我有一小段代码,如下所示,用于列出所有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");
}
系统信息
我把我的解决方案分享给将来需要的人。
找到你要找的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();