2012-05-30 33 views
0

可能重複:
How to check if a dll is registered?如何找到在C#中安裝DLL

我需要找到本地機器上是否有安裝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); 
} 

這是做了正確的方法是什麼?

+0

可能重複:http://stackoverflow.com/questions/689072/how-to-check-if-a-dll-is-registered或http://stackoverflow.com/問題/ 377551/physicalinstalled -dll-installed-to-the-gac的路徑 –

+0

根據它給出的程序集包含當前正在執行的代碼。但我需要特定的DLL。 – DevT

+0

通過使用註冊表我們不能這樣做。 :) – DevT

回答

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