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

Gok*_*nan 12 c#

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

dva*_*ejo 14

做一个assembly.LoadFrom并检查GlobalAssemblyCache

testAssembly = Assembly.LoadFrom(dllname);

if (!testAssembly.GlobalAssemblyCache)
{
  // not in gac
}
Run Code Online (Sandbox Code Playgroud)


Inc*_*ito 9

我认为正确的方法是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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用QueryAssemblyInfo方法.


Tim*_*son 3

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

此 API 设计为从本机代码使用,因此本文可能会帮助您从 C# 对其进行编程。

如果您正在寻求快速解决方案,gacutil /l那么可行。