19.3. List What Processes an Assembly Is Loaded In
Problem
You want to know what current processes have a given assembly loaded.
Solution
Use the GetProcessesAssemblyIsLoadedIn method that we've created for this purpose to return a list of processes that a given assembly is loaded in. GetProcessesAssemblyIsLoadedIn takes the filename of the assembly to look for (such as System.Data.dll), and then gets a list 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 projected into an enumerable set of Process objects. The iterator for the set of processes found is returned from the query:
public static IEnumerable<Process> GetProcessesAssemblyIsLoadedIn(
string assemblyFileName)
{
var processes = from process in Process.GetProcesses()
where process.ProcessName != "System" &&
process.ProcessName != "Idle"
from ProcessModule processModule in process.Modules
where processModule.ModuleName.Equals(assemblyFileName,
StringComparison.OrdinalIgnoreCase)
select process;
return processes;
}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 process. 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access