枚举程序集的所有已安装版本(在 GAC 中) [英] Enumerate all installed versions of an assembly (in GAC)
问题描述
是否可以使用 C# 在 GAC 中枚举所有已安装的程序集版本?例如,我有一个名为My.Assembly"的程序集.该程序集可能有各种版本(1.0.0.0"、2.3.4.5"、0.1.2.4"、...),并且可以针对各种平台(x86、x64、任何 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 中检索一些信息(不加载它),然后必须启动处理 DLL 的正确托管 C# 应用程序.可以为 Win32 或 x64 编译 DLL 将始终公开相同的(独立于平台的)接口.我使用 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.
推荐答案
使用非托管 Fusion API 的 rel="noreferrer">托管包装a> a 能够做我想做的事:
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
感谢 Hans Passant 为我指明了正确的方向!
Thanks to Hans Passant who pointed me in the right direction!
这篇关于枚举程序集的所有已安装版本(在 GAC 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!