List What Processes an Assembly Is Loaded In






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 (like System.Data.dll), 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 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 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 = "System.Data.dll";
	List<Process> processes = Toolbox.GetProcessesAssemblyIsLoadedIn(searchAssm);
	foreach (Process p in processes)
	{
	    Console.WriteLine("Found {0} in {1}",searchAssm, p.MainModule.ModuleName);
	}

The preceding code might produce output like this (you may see more if you have other applications running):

	Found System.Data.dll in WebDev.WebServer.EXE
	Found System.Data.dll in devenv.exe
	Found System.Data.dll in CSharpRecipes.vshost.exe

Since this is a diagnostic function, you will need FullTrust security access to use this method.

See Also

See the "Process Class," "ProcessModule Class," and "GetProcesses Method" topics in the MSDN documentation.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows