如何枚举正在运行的进程? app域怎么样?
会不会有任何与安全相关的陷阱?
我对仅使用.NET 4.0,仅支持Windows 7 / Windows Server 2008 R2的解决方案感到满意。
P.S。:这就是我想要做的...... ProcessExplorer.NET question
答案 0 :(得分:2)
答案 1 :(得分:1)
System.Diagnostics.Process.GetProcesses()会将所有正在运行的进程列为Process个对象。
答案 2 :(得分:1)
使用上面的Process.GetProcesses后,您可以使用ICLRMetaHost :: EnumerateLoadedRuntimes来查看进程是否正在运行CLR的实例,并获取其中运行的CLR版本。 http://msdn.microsoft.com/en-us/library/dd233119.aspx。 API不直接暴露给.NET,但它是COM,因此您可以使用mscoree.dll并在.NET中使用自动生成的包装器。
答案 3 :(得分:1)
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
string processName = proc.ProcessName;
int processId = proc.Id;
try
{
if (proc.HasDesktop40CLR())
Console.WriteLine("#{0}, {1} (has Desktop CLR 4.0+)", processId, processName);
if (proc.HasOlderDesktopCLR())
Console.WriteLine("#{0}, {1} (has Older Desktop CLR)", processId, processName);
if (proc.HasMscorlib())
Console.WriteLine("#{0}, {1} (has MSCORLib)", processId, processName);
}
catch (Exception)
{
Console.WriteLine("#{0}, {1} (skipped/unknown)", processId, processName);
}
}
}
}
public static class ProcessExtensions
{
public static bool HasDesktop40CLR(this Process proc)
{
for (int ix = 0; ix < proc.Modules.Count; ++ix)
{
var moduleName = proc.Modules[ix].ModuleName;
if (string.Compare(moduleName, "mscoree.dll", ignoreCase: true) == 0)
return true;
if (string.Compare(moduleName, "mscoreei.dll", ignoreCase: true) == 0)
return true;
}
return false;
}
public static bool HasOlderDesktopCLR(this Process proc)
{
for (int ix = 0; ix < proc.Modules.Count; ++ix)
{
var moduleName = proc.Modules[ix].ModuleName;
if (string.Compare(moduleName, "mscorwks.dll", ignoreCase: true) == 0)
return true;
}
return false;
}
public static bool HasMscorlib(this Process proc)
{
for (int ix = 0; ix < proc.Modules.Count; ++ix)
{
var moduleName = proc.Modules[ix].ModuleName;
if (string.Compare(moduleName, "mscorlib.dll", ignoreCase: true) == 0)
return true;
if (string.Compare(moduleName, "mscorlib.ni.dll", ignoreCase: true) == 0)
return true;
}
return false;
}
}
}