枚举所有已安装的组件的版本(在GAC) [英] Enumerate all installed versions of an assembly (in GAC)

查看:146
本文介绍了枚举所有已安装的组件的版本(在GAC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能列举的组件在GAC使用C#所有已安装的版本?比如我有一个名为My.Assembly大会。该组件可有各种版本(1.0.0.0,2.3.4.5,0.1.2.4,...),并可以编译为各种平台(86,64,任何CPU)。

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

现在我需要一种方法来确定这些版本哪个/安装平台。

Now I need a way to determine which of the versions/platforms are installed.

我知道,我可以枚举GAC的目录,但似乎是错误的。应该有更好的方式来做到这一点。

I'm aware that I could enumerate the directories in GAC but that seems wrong. There should be a better way to do this.

背景 我在其中用户选择一个DLL一个启动器应用。该发射器检索出来的DLL的一些信息(不加载它),然后已经开始正确的托管C#应用程序,它处理的DLL。该DLL可能会被编译为Win32或x64​​看跌总是公开相同的(独立于平台)接口。我用的是LoadLibrary函数加载在C#应用程序了的DLL。唯一的问题是,该工艺是匹配的格式(x86或x64)的。 C#应用程序可以而且应该被编译为x86,x64和任何CPU。

Background I have a launcher application in which the user selects a DLL. The launcher retrieves some information from the DLL (without loading it) and then has to start the correct managed C# application which handles the DLL. The DLL may be compiled for Win32 or x64 put exposes always the same (platform independent) interface. I use the LoadLibrary function to load the DLL in the C# applicaiton. The only problem is that the process has to be of matching format (x86 or x64). The C# application can and should be compiled for x86, x64 and Any CPU.

推荐答案

使用的托管包装不受管理的融合API 一个是能够​​做的正是我想做的事:

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!

这篇关于枚举所有已安装的组件的版本(在GAC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆