使用managed wrapper爲Unmanaged Fusion API一個能夠做正是我想做的事:
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);
}
}
運行上面的程序給我下面的輸出:
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
感謝漢斯帕桑特誰指出我在正確的方向!
您可以使用Fusion api,CreateAssemblyEnum()和IAssemblyEnum + IAssemblyName執行此操作。這不是在.NET框架中公開的,但它包含了它的代碼。 ILSpy或Reflector將會很方便。 –
@HansPassant融合似乎正是我所期待的。這似乎很難使用。如果有管理/內置的支持這種事情會很好。非常感謝! – Korexio