以编程方式检查Gac中是否存在dll。如果将其显示在网格中

我将在一个文件夹中有一个dll列表,我想检查一个dll是否存在应用程序。 如果是这样,我想在grid中添加该应用程序名称。任何人都可以通过编程方式告诉它如何进行。 提前致谢

做一个assembly.LoadFrom并检查GlobalAssemblyCache

testAssembly = Assembly.LoadFrom(dllname); if (!testAssembly.GlobalAssemblyCache) { // not in gac } 

我认为正确的方法是Fusion COM API。

在这里如何使用它:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace IsAssemblyInGAC { internal class GacApi { [DllImport("fusion.dll")] internal static extern IntPtr CreateAssemblyCache( out IAssemblyCache ppAsmCache, int reserved); } // GAC Interfaces - IAssemblyCache. As a sample, non used vtable entries [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")] internal interface IAssemblyCache { int Dummy1(); [PreserveSig()] IntPtr QueryAssemblyInfo( int flags, [MarshalAs(UnmanagedType.LPWStr)] String assemblyName, ref ASSEMBLY_INFO assemblyInfo); int Dummy2(); int Dummy3(); int Dummy4(); } [StructLayout(LayoutKind.Sequential)] internal struct ASSEMBLY_INFO { public int cbAssemblyInfo; public int assemblyFlags; public long assemblySizeInKB; [MarshalAs(UnmanagedType.LPWStr)] public String currentAssemblyPath; public int cchBuf; } class Program { static void Main() { try { Console.WriteLine(QueryAssemblyInfo("System")); } catch(System.IO.FileNotFoundException e) { Console.WriteLine(e.Message); } } // If assemblyName is not fully qualified, a random matching may be public static String QueryAssemblyInfo(String assemblyName) { ASSEMBLY_INFO assembyInfo = new ASSEMBLY_INFO (); assembyInfo.cchBuf = 512; assembyInfo.currentAssemblyPath = new String('\0', assembyInfo.cchBuf) ; IAssemblyCache assemblyCache = null; // Get IAssemblyCache pointer IntPtr hr = GacApi.CreateAssemblyCache(out assemblyCache, 0); if (hr == IntPtr.Zero) { hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref assembyInfo); if (hr != IntPtr.Zero) { Marshal.ThrowExceptionForHR(hr.ToInt32()); } } else { Marshal.ThrowExceptionForHR(hr.ToInt32()); } return assembyInfo.currentAssemblyPath; } } } 

使用QueryAssemblyInfo方法。

以下是未记录的GAC API的文档: DOC:全局程序集缓存(GAC)API未在.NET Framework软件开发工具包(SDK)文档中记录 。

此API旨在从本机代码中使用,因此本文可能会帮助您从C#编程。

如果你正在快速解决方案, gacutil /l工作。

通常人们会假设GAC位于c:\\ assembly中,这是99%的真实。 因此,您可以编写代码来遍历该文件夹中的文件。

更正统的方法是使用基于COM的Fusion API。 此博客站点上提供了托管包装器:

http://blogs.msdn.com/junfeng/archive/2004/09/14/229653.aspx

该站点还包含示例托管代码,显示如何使用Fusion API枚举已安装的程序集 – 代码几乎是为您编写的,所以只需按ctrl + c然后按ctrl + v … 🙂

http://blogs.msdn.com/b/junfeng/archive/2004/09/14/229650.aspx

HTH …

我知道这是一个老问题,但为了新旅行者的缘故…

我们有一个(免费)融合库可以从http://dilithium.co.za/info/products/gac-api/下载,它就是这样做的。 披露:是的,我是附属的。 不,如果你下载,我不会得到钱。 😉