enumerating assemblies in GAC

后端 未结 8 1861
故里飘歌
故里飘歌 2020-12-05 16:14

How can I enumerate all available assemblies in GAC in C#?

Actually I am facing an issue with a stupid code - the assembly called Telerik.Web.UI.dll is referred and

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 16:29

    a simple method to enumerate dll files in a directory

    public static string[] GetGlobalAssemblyCacheFiles(string path)
    {
        List files = new List();
    
        DirectoryInfo di = new DirectoryInfo(path);
    
        foreach (FileInfo fi in di.GetFiles("*.dll"))
        {
            files.Add(fi.FullName);
        }
    
        foreach (DirectoryInfo diChild in di.GetDirectories())
        {
            var files2 = GetGlobalAssemblyCacheFiles(diChild.FullName);
            files.AddRange(files2);
        }
    
        return files.ToArray();
    }
    

    and you can get all files

    string gacPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Windows) + "\\assembly";
    var files = GetGlobalAssemblyCacheFiles(gacPath);
    

    If you need to load each assembly, You get a exception for different run-time version. For this reason you can load assembly with Assembly.ReflectionOnlyLoadFrom to load Assembly in reflection only. Then, Assembly don't load in your AppDomain and don't throw exception.

提交回复
热议问题