Check GAC for an assembly

不羁的心 提交于 2019-11-28 09:39:29

Without even trying to get complicated, you could just shell out to gacutil and capture the output. For example, gacutil /l Microsoft.Practices.Unity gives me:

Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

The Global Assembly Cache contains the following assemblies:
  Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31
bf3856ad364e35, processorArchitecture=MSIL

Number of items = 1

versus gacutil /l Some.Nonexistant.Assembly:

Microsoft (R) .NET Global Assembly Cache Utility.  Version 3.5.30729.1
Copyright (c) Microsoft Corporation.  All rights reserved.

The Global Assembly Cache contains the following assemblies:

Number of items = 0

This is easy to implement and parse and isn't dependent on any third-party implementations.

BALKANGraph

It's better to use ReflectionOnlyLoad Method. this method loads an assembly into the reflection-only context, where it can be examined but not executed.

StrayPointer

From .NET, the reflection API - Assembly.Load(...) will throw a FileNotFoundException if it does not find the assembly. The API requires a fully qualified assembly name, so I assume it must be in the GAC. I am using it to test for the presence of SQL Server Compact Edition:

Assembly foo = Assembly.Load("System.Data.SqlServerCe, Version=3.5.1.0, " +
    "Culture=neutral, PublicKeyToken=89845dcd8080cc91");

You can use the Fusion COM API. Junfeng Zhang wrote a managed wrapper. It's from 2004, though, so I don't know how well it works anymore.

Do you want to probe the GAC for an assembly or do you just want to know that the assembly exists on the machine?

If you dont care that the assembly is actually in the GAC, but just loadable on the machine (from the appdomain) you can just use LoadAssembly with the assemblies name (strong, common, full, etc). If the assembly can be loaded by Fusion it will be and then you will know it exists.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!