List all installed build versions (in the GAC)

Is it possible to list all installed versions of an assembly in the GAC using C #? For example, I have an assembly named "My.Assembly". The assembly can be delivered in various versions ("1.0.0.0", "2.3.4.5", "0.1.2.4", ...) and can be compiled for different platforms (x86, x64, Any CPU).

Now I need to determine which version / platform is installed.

I know that I could list directories in the GAC, but that seems wrong. There must be a better way to do this.

Background I have a launch application in which the user selects a DLL. The launcher extracts some information from the DLL (without loading it), and then launches the correct C # managed application that processes the DLL. A DLL can be compiled for Win32 or x64 put always provides the same (platform independent) interface. I am using the LoadLibrary function to load a DLL into a C # application. The only problem is that this process must conform to the format (x86 or x64). A C # application can and should be compiled for x86, x64, and Any CPU.

+4
source share
1 answer

Using a managed wrapper for the Unmanaged Fusion API, a was able to do exactly what I wanted to do:

class Program { static IEnumerable<AssemblyName> GetInstalledVersions(string name) { int result; IAssemblyName assemblyName; result = Utils.CreateAssemblyNameObject(out assemblyName, name, CreateAssemblyNameObjectFlags.CANOF_DEFAULT, IntPtr.Zero); if ((result != 0) || (assemblyName == null)) throw new Exception("CreateAssemblyNameObject failed."); IAssemblyEnum enumerator; result = Utils.CreateAssemblyEnum(out enumerator, IntPtr.Zero, assemblyName, AssemblyCacheFlags.GAC, IntPtr.Zero); if ((result != 0) || (enumerator == null)) throw new Exception("CreateAssemblyEnum failed."); while ((enumerator.GetNextAssembly(IntPtr.Zero, out assemblyName, 0) == 0) && (assemblyName != null)) { StringBuilder displayName = new StringBuilder(1024); int displayNameLength = displayName.Capacity; assemblyName.GetDisplayName(displayName, ref displayNameLength, (int)AssemblyNameDisplayFlags.ALL); yield return new AssemblyName(displayName.ToString()); } } static void Main(string[] args) { foreach (AssemblyName assemblyName in GetInstalledVersions("System.Data")) Console.WriteLine("{0} V{1}, {2}", assemblyName.Name, assemblyName.Version.ToString(), assemblyName.ProcessorArchitecture); } } 

Running the program above gives me the following output:

 System.Data V2.0.0.0, Amd64 System.Data V2.0.0.0, X86 System.Data V4.0.0.0, Amd64 System.Data V4.0.0.0, X86 

Thanks to Hans Passant who pointed me in the right direction!

+3
source

All Articles