这个问题是非常类似于下面的问题,但我的是一个更确切的一点,再加上既没有一个公认的答案,并没有提供答案似乎完全或最佳:
最初我以为以下是最好的方法,但它不工作,除非你指定程序集的全名,这是一种hacky因为try/catch,但它很简单而工作在很多情况下:
public static class GacUtil
{
public static bool IsAssemblyInGAC(string assemblyFullName)
{
try
{
return Assembly.ReflectionOnlyLoad(assemblyFullName)
.GlobalAssemblyCache;
}
catch
{
return false;
}
}
public static bool IsAssemblyInGAC(Assembly assembly)
{
return assembly.GlobalAssemblyCache;
}
}
这是一个更好的办法,没有一个try/catch使用Fusion API工作。这是一帮更多的代码,但它与局部配件名称的工作原理:
public static class GacUtil
{
[DllImport("fusion.dll")]
private static extern IntPtr CreateAssemblyCache(
out IAssemblyCache ppAsmCache,
int reserved);
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
private interface IAssemblyCache
{
int Dummy1();
[PreserveSig()]
IntPtr QueryAssemblyInfo(
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string assemblyName,
ref AssemblyInfo assemblyInfo);
int Dummy2();
int Dummy3();
int Dummy4();
}
[StructLayout(LayoutKind.Sequential)]
private struct AssemblyInfo
{
public int cbAssemblyInfo;
public int assemblyFlags;
public long assemblySizeInKB;
[MarshalAs(UnmanagedType.LPWStr)]
public string currentAssemblyPath;
public int cchBuf;
}
public static bool IsAssemblyInGAC(string assemblyName)
{
var assembyInfo = new AssemblyInfo { cchBuf = 512 };
assembyInfo.currentAssemblyPath = new string('\0', assembyInfo.cchBuf);
IAssemblyCache assemblyCache;
var hr = CreateAssemblyCache(out assemblyCache, 0);
if (hr == IntPtr.Zero)
{
hr = assemblyCache.QueryAssemblyInfo(
1,
assemblyName,
ref assembyInfo);
if (hr != IntPtr.Zero)
{
return false;
}
return true;
}
Marshal.ThrowExceptionForHR(hr.ToInt32());
return false;
}
}
可能重复http://stackoverflow.com/ questions/1933947/check-gac-for-an-assembly) – JMK
使用fusion api,[IAssemblyCache.QueryAssemblyInfo](http://stackoverflow.com/a/2611435/17034)。 –
@Hans我可能应该指定我想要一个完全托管的解决方案,但你是正确的,Fusion API也会做我想做的。查看我的答案,了解我最终为完全托管解决方案所做的工作。 –