Enumerating AppDomains

Have you ever wondered how to enumerate all AppDomains in the current process?  It can be done with a little COM interop...

// Add the following as a COM reference...

// C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\mscoree.tlb

using System;

using System.Collections;

using System.Runtime.InteropServices;

using mscoree;

public class AppDomainUtils

{

public static IList ListAppDomains()

{

CorRuntimeHostClass host = new CorRuntimeHostClass();

try

{

ArrayList list = new ArrayList();

IntPtr enumHandle;

host.EnumDomains(out enumHandle);

while(true)

{

object domain;

host.NextDomain(enumHandle, out domain);

if(domain == null) break;

list.Add((AppDomain)domain);

}

host.CloseEnum(enumHandle);

return list;

}

finally

{

Marshal.ReleaseComObject(host);

}

}

}

This is a modified version of some code NETMaster (Thomas Scheidegger) posted.

3 Comments

Comments have been disabled for this content.