enumerating assemblies in GAC

后端 未结 8 1850
故里飘歌
故里飘歌 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:21

    Figure i might as well throw in my linq based solution (handy if your just looking for a single file and don't want to enumerate through every file)

    function to find the files:

        public static IEnumerable FindFiles (string path, string filter = "", int depth = int.MaxValue) {
            DirectoryInfo di = new DirectoryInfo(path);
    
            IEnumerable results = (! string.IsNullOrEmpty(filter) ? di.GetFiles(filter) : di.GetFiles()).Select(f => f.FullName);
            if (depth > 0) {
                results = results.Concat(di.GetDirectories().SelectMany(d => FindFiles(d.FullName, filter, depth - 1)));
            }
    
            return results;
        }
    

    And to call (i didn't have Environment.SpecialFolder.Windows so used Environment.SpecialFolder.System instead)

    string path = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\..\\assembly";
    foreach (string s in FindFiles(path, "*.dll")) {
         Console.WriteLine(s);
    }
    

    Further, as a FYI this method enumerates the entire GAC and you may find duplicate DLLs in tmp/ and temp/, Tmp is used for installation, and Temp is used for uninstallation.

提交回复
热议问题