如何在 C# 中查找安装 DLL

本文关键字:查找 安装 DLL | 更新日期: 2023-09-27 18:35:39

可能的重复项:
如何检查 dll 是否已注册?

我需要查找本地计算机是否安装了SlimDX.dll。如何获取安装 dll 的列表?dll 的路径是 -> C:''Windows''Microsoft.NET''assembly''GAC_32

string  path="";
rk = Registry.LocalMachine.OpenSubKey("");
 string[] keys = rk.GetSubKeyNames();
foreach (string keyName in keys)
 {
       RegistryKey subKey = rk.OpenSubKey(keyName);
 }

这是正确的方法吗?

如何在 C# 中查找安装 DLL

你可以看看这里:

"以编程方式检查程序集是否使用 C# 在 GAC 中加载"

如果链接变坏:

public static bool AssemblyExist(string assemblyname, out string response)
{
    try
    {
        response = QueryAssemblyInfo(assemblyname);
        return true;
    }
    catch (System.IO.FileNotFoundException e)
    {
        response = e.Message;
        return false;
    }
}
// If assemblyName is not fully qualified, a random matching may be 
public static String QueryAssemblyInfo(string assemblyName)
{
    var assembyInfo = new AssemblyInfo {cchBuf = 512};
    assembyInfo.currentAssemblyPath = new String('', assembyInfo.cchBuf);
    IAssemblyCache assemblyCache;
    // Get IAssemblyCache pointer
    var 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;
}

可以通过以下代码获取 GAC 中可用 DLL 的列表。 有关更多详细信息,请访问 http://www.developerfusion.com/thread/42116/getting-list-of-assemblies-in-gac/

string strGacDir = @"C:'Windows'Microsoft.NET'assembly'GAC_32";
string[] strDirs1 = System.IO.Directory.GetDirectories(strGacDir);
string[] strDirs2;
string[] strFiles;
foreach (string strDir1 in strDirs1)
{
    strDirs2 = System.IO.Directory.GetDirectories(strDir1);
    foreach (string strDir2 in strDirs2)
    {
        strFiles = System.IO.Directory.GetFiles(strDir2, "SlimDX.dll");
        foreach (string strFile in strFiles)
        {
            return true;
        }
    }
}