By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,685 Members | 1,243 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to enumerate AppDomains in current process

Expert 100+
P: 750
Here is a code i found on "how to enumerate appdomains in a current process".
The original code was posted by Thomas Scheidegger


Add the following as a COM reference - ~\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscor ee.tlb

Expand|Select|Wrap|Line Numbers
  1.  
  2. using mscoree;
  3. using System.Runtime.InteropServices;
  4.  
  5. public void GetAllAppDomains()
  6.         {
  7.  
  8.             AppDomain one = AppDomain.CreateDomain("One");
  9.             AppDomain two = AppDomain.CreateDomain("Two"); 
  10.            // Creates 2 app domains           
  11.  
  12.             List<AppDomain> appDomains = new List<AppDomain>();
  13.  
  14.             IntPtr enumHandle = IntPtr.Zero;
  15.  
  16.             CorRuntimeHostClass host = new CorRuntimeHostClass();          
  17.  
  18.             try
  19.             {
  20.  
  21.                 host.EnumDomains(out enumHandle);
  22.  
  23.                 object domain = null;
  24.  
  25.                 AppDomain tempDomain;
  26.  
  27.                 while (true)
  28.                 {
  29.  
  30.                     host.NextDomain(enumHandle, out domain);
  31.  
  32.                     if (domain == null)
  33.                     {
  34.                         break;
  35.                     }
  36.  
  37.                     tempDomain = domain as AppDomain;
  38.  
  39.                     appDomains.Add(tempDomain);
  40.  
  41.                 }               
  42.  
  43.             }
  44.  
  45.             catch (Exception ex)
  46.             {
  47.                 Console.WriteLine(ex.ToString());          
  48.             }
  49.  
  50.             finally
  51.             {
  52.                 host.CloseEnum(enumHandle);
  53.                 int rel= Marshal.ReleaseComObject(host);
  54.             }
  55.  
  56.             Assembly[] assemblies;
  57.             foreach (AppDomain app in appDomains)
  58.             {
  59.                 Console.WriteLine(app.FriendlyName);
  60.  
  61.                 assemblies = app.GetAssemblies();
  62.  
  63.                 Console.WriteLine("-----------------------Assemblies------------------");
  64.                 foreach (Assembly assem in assemblies)
  65.                 {
  66.                     Console.WriteLine(assem.FullName);
  67.                 }
  68.                 Console.WriteLine("---------------------------------------------------");
  69.             }
  70.  
  71.         }
  72.  
Nov 15 '08 #1
Share this Article
Share on Google+