
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1092
|
Chapter 20: Toolbox
of the currently running processes on the machine by calling Process.GetProcesses.
It then searches the processes to see if the assembly is loaded into any of them. When
found in a process, that
Process object is added to a List<Process>. The entire
List<Process> is returned once all the processes have been examined.
public static List<Process> GetProcessesAssemblyIsLoadedIn(string assemblyFileName)
{
List<Process> processList = new List<Process>( );
Process[] processes = Process.GetProcesses( );
foreach (Process p in processes)
{
foreach (ProcessModule module in p.Modules)
{
if (module.ModuleName.Equals(assemblyFileName,
StringComparison.OrdinalIgnoreCase))
{
processList.Add(p);
break;
}
}
}
return processList;
}
Discussion
In some circumstances, such as when uninstalling software or debugging version
conflicts, it is beneficial to know if an assembly is loaded into more than one pro-
cess. By quickly getting a list of the
Process objects that the assembly is loaded in,
you can narrow the scope of your investigation.
The following code uses this routine:
string searchAssm = "System.Data.dll";
List<Process> processes = Toolbox.GetProcessesAssemblyIsLoadedIn(searchAssm);
foreach (Process p in processes)
{
Console.WriteLine("Found {0} in {1}",searchAssm, p.MainModule.ModuleName); ...