如何在C#中找到安装dll

时间:2012-05-30 08:17:40

标签: c# c#-4.0

  

可能重复:
  How to check if a dll is registered?

我需要查找本地计算机是否安装了SlimDX.dll。我怎样才能获得安装dll的列表? dll的路径是 - > C:\的Windows \ Microsoft.NET \组件\ GAC_32

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

这是正确的方法吗?

2 个答案:

答案 0 :(得分:1)

你可以在这看看:

Programmatically check if an assembly is loaded in GAC with C#

如果链接变坏:

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;
}

答案 1 :(得分:1)

可以通过以下代码获取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;
        }
    }
}