You can use WMI, here is a snippet of code that receives all processes and relative modules:
var wmiQueryString = string.Format("select * from CIM_ProcessExecutable");
Dictionary<int, ProcInfo> procsMods = new Dictionary<int, ProcInfo>();
using (var searcher = new ManagementObjectSearcher(string.Format(wmiQueryString)))
using (var results = searcher.Get())
{
foreach (var item in resMg.Cast<ManagementObject>())
{
try
{
var antecedent = new ManagementObject((string)item["Antecedent"]);
var dependent = new ManagementObject((string)item["Dependent"]);
int procHandleInt = Convert.ToInt32(dependent["Handle"]);
ProcInfo pI = new ProcInfo { Handle = procHandleInt, FileProc = new FileInfo((string)dependent["Name"]) };
if (!procsMods.ContainsKey(procHandleInt))
{
procsMods.Add(procHandleInt, pI);
}
procsMods[procHandleInt].Modules.Add(new ModInfo { FileMod = new FileInfo((string)antecedent["Name"]) });
}
catch (System.Management.ManagementException ex)
{
}
}
}
In procsMods
we saved the processes and modules, now we print them:
foreach (var item in procsMods)
{
Console.WriteLine(string.Format("{0} ({1}):", item.Value.FileProc.Name, item.Key));
foreach (var mod in item.Value.Modules)
{
Console.WriteLine("\t{0}", mod.FileMod.Name);
}
}
ProcInfo
ModInfo
:
class ProcInfo
{
public FileInfo FileProc { get; set; }
public int Handle { get; set; }
public List<ModInfo> Modules { get; set; }
public ProcInfo()
{
Modules = new List<ModInfo>();
}
}
class ModInfo
{
public FileInfo FileMod { get; set; }
}