none
Obtaining List of Assemblies present in GAC

    Question

  • Hi All..

    I want to obtain the List of Assemblies present in GAC, programatically through C#.. Is it possible?
    What should I do to implement it?

    Regards,
    Prash
    Tuesday, July 22, 2008 11:17 AM

Answers

  •  Here the solution:
    1 /// <summary>  
    2     /// Global Assembly Cache manager  
    3     /// </summary>  
    4     public sealed class GlobalAssemblyCache: ICollection<AssemblyName>  
    5     {  
    6         private readonly Process m_gacutil;  
    7         //Path to the GACUTIL tool  
    8         private const string GacUtilExe = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe";  
    9  
    10         public GlobalAssemblyCache()  
    11         {  
    12             m_gacutil = new Process();  
    13             m_gacutil.StartInfo.FileName = GacUtilExe;  
    14             m_gacutil.StartInfo.RedirectStandardOutput = true;  
    15             m_gacutil.StartInfo.UseShellExecute = false;  
    16         }
    17
    18         #region ICollection<AssemblyName> Members  
    19  
    20         /// <summary>  
    21         /// Add a new assembly to the GAC  
    22         /// </summary>  
    23         /// <param name="item">Display name of the assembly</param>  
    24         public void Add(AssemblyName item)  
    25         {  
    26             if (item == null)  
    27                 throw new ArgumentNullException("item");  
    28             m_gacutil.StartInfo.Arguments = String.Format("/i \"{0}\"", item.CodeBase);  
    29             m_gacutil.Start();  
    30         }  
    31  
    32         /// <summary>  
    33         /// Delete all assemblies from GAC  
    34         /// </summary>  
    35         void ICollection<AssemblyName>.Clear()  
    36         {  
    37             foreach (var item in this)  
    38                 Remove(item);  
    39         }  
    40  
    41         /// <summary>  
    42         /// Returns value indicating that assembly with specified name is already installed in the GAC  
    43         /// </summary>  
    44         /// <param name="item">Assembly name</param>  
    45         /// <returns>true, if assembly is installed, otherwise, false</returns>  
    46         public bool Contains(AssemblyName item)  
    47         {  
    48             if (item == nullreturn false;  
    49             var queryResult = from asm in this 
    50                               where AssemblyName.ReferenceMatchesDefinition(item, asm)  
    51                               select true;  
    52             return queryResult.FirstOrDefault();  
    53         }  
    54  
    55         void ICollection<AssemblyName>.CopyTo(AssemblyName[] array, int arrayIndex)  
    56         {  
    57             this.ToArray().CopyTo(array, arrayIndex);  
    58         }  
    59  
    60         /// <summary>  
    61         /// Get count of assemblies in the GAC  
    62         /// </summary>  
    63         public int Count  
    64         {  
    65             get { return this.Count(); }  
    66         }  
    67  
    68  
    69         bool IsReadOnly  
    70         {  
    71             get { return false; }  
    72         }  
    73  
    74         /// <summary>  
    75         /// Remove assembly from GAC  
    76         /// </summary>  
    77         /// <param name="item"></param>  
    78         /// <returns></returns>  
    79         public bool Remove(AssemblyName item)  
    80         {  
    81             if (item == nullreturn false;  
    82             m_gacutil.StartInfo.Arguments = String.Format("/u \"{0}\"", item);  
    83             m_gacutil.Start();  
    84         }
    85
    86         #endregion  
    87  
    88         #region IEnumerable<AssemblyName> Members  
    89  
    90         /// <summary>  
    91         /// Get GAC iterator  
    92         /// </summary>  
    93         /// <returns></returns>  
    94         public IEnumerator<AssemblyName> GetEnumerator()  
    95         {  
    96             m_gacutil.StartInfo.Arguments = "/l";  
    97             m_gacutil.Start();  
    98             var output = m_gacutil.StandardOutput;  
    99             while (!output.EndOfStream)  
    100                 yield return CreateAssemblyNameSafe(output.ReadLine());  
    101         }
    102
    103         #endregion  
    104  
    105         #region IEnumerable Members  
    106  
    107         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
    108         {  
    109             return GetEnumerator();  
    110         }
    111
    112         #endregion  
    113  
    114         private AssemblyName CreateAssemblyNameSafe(string assemblyName)  
    115         {  
    116             var result = default(AssemblyName);  
    117             try 
    118             {  
    119                 result = new AssemblyName(assemblyName);  
    120             }  
    121             catch 
    122             {  
    123                 result = null;  
    124             }  
    125             return result;  
    126         }  
    127     } 
    And now you can enumerate all assemblies in the GAC:
    1 static void Main(string[] args)  
    2         {  
    3             foreach(var asm in new GlobalAssemblyCache())  
    4             {  
    5                 Console.WriteLine(asm);  
    6             }  
    7             Console.ReadKey();  
    8         } 

    E' più facile spezzare un atomo che un pregiudizio
    Wednesday, July 23, 2008 1:15 AM
  • That's not possible to do directly.  You could capture the output of gacutil /l
    Hans Passant.
    Tuesday, July 22, 2008 12:43 PM
    Moderator

All replies

  • That's not possible to do directly.  You could capture the output of gacutil /l
    Hans Passant.
    Tuesday, July 22, 2008 12:43 PM
    Moderator
  •  Thanks for your valuable reply.
    Can we use gacutil commnad inside C# code to get that list, and how?
    Tuesday, July 22, 2008 1:50 PM
  • Use the Process class.
    Hans Passant.
    Tuesday, July 22, 2008 3:46 PM
    Moderator
  •  Here the solution:
    1 /// <summary>  
    2     /// Global Assembly Cache manager  
    3     /// </summary>  
    4     public sealed class GlobalAssemblyCache: ICollection<AssemblyName>  
    5     {  
    6         private readonly Process m_gacutil;  
    7         //Path to the GACUTIL tool  
    8         private const string GacUtilExe = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe";  
    9  
    10         public GlobalAssemblyCache()  
    11         {  
    12             m_gacutil = new Process();  
    13             m_gacutil.StartInfo.FileName = GacUtilExe;  
    14             m_gacutil.StartInfo.RedirectStandardOutput = true;  
    15             m_gacutil.StartInfo.UseShellExecute = false;  
    16         }
    17
    18         #region ICollection<AssemblyName> Members  
    19  
    20         /// <summary>  
    21         /// Add a new assembly to the GAC  
    22         /// </summary>  
    23         /// <param name="item">Display name of the assembly</param>  
    24         public void Add(AssemblyName item)  
    25         {  
    26             if (item == null)  
    27                 throw new ArgumentNullException("item");  
    28             m_gacutil.StartInfo.Arguments = String.Format("/i \"{0}\"", item.CodeBase);  
    29             m_gacutil.Start();  
    30         }  
    31  
    32         /// <summary>  
    33         /// Delete all assemblies from GAC  
    34         /// </summary>  
    35         void ICollection<AssemblyName>.Clear()  
    36         {  
    37             foreach (var item in this)  
    38                 Remove(item);  
    39         }  
    40  
    41         /// <summary>  
    42         /// Returns value indicating that assembly with specified name is already installed in the GAC  
    43         /// </summary>  
    44         /// <param name="item">Assembly name</param>  
    45         /// <returns>true, if assembly is installed, otherwise, false</returns>  
    46         public bool Contains(AssemblyName item)  
    47         {  
    48             if (item == nullreturn false;  
    49             var queryResult = from asm in this 
    50                               where AssemblyName.ReferenceMatchesDefinition(item, asm)  
    51                               select true;  
    52             return queryResult.FirstOrDefault();  
    53         }  
    54  
    55         void ICollection<AssemblyName>.CopyTo(AssemblyName[] array, int arrayIndex)  
    56         {  
    57             this.ToArray().CopyTo(array, arrayIndex);  
    58         }  
    59  
    60         /// <summary>  
    61         /// Get count of assemblies in the GAC  
    62         /// </summary>  
    63         public int Count  
    64         {  
    65             get { return this.Count(); }  
    66         }  
    67  
    68  
    69         bool IsReadOnly  
    70         {  
    71             get { return false; }  
    72         }  
    73  
    74         /// <summary>  
    75         /// Remove assembly from GAC  
    76         /// </summary>  
    77         /// <param name="item"></param>  
    78         /// <returns></returns>  
    79         public bool Remove(AssemblyName item)  
    80         {  
    81             if (item == nullreturn false;  
    82             m_gacutil.StartInfo.Arguments = String.Format("/u \"{0}\"", item);  
    83             m_gacutil.Start();  
    84         }
    85
    86         #endregion  
    87  
    88         #region IEnumerable<AssemblyName> Members  
    89  
    90         /// <summary>  
    91         /// Get GAC iterator  
    92         /// </summary>  
    93         /// <returns></returns>  
    94         public IEnumerator<AssemblyName> GetEnumerator()  
    95         {  
    96             m_gacutil.StartInfo.Arguments = "/l";  
    97             m_gacutil.Start();  
    98             var output = m_gacutil.StandardOutput;  
    99             while (!output.EndOfStream)  
    100                 yield return CreateAssemblyNameSafe(output.ReadLine());  
    101         }
    102
    103         #endregion  
    104  
    105         #region IEnumerable Members  
    106  
    107         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
    108         {  
    109             return GetEnumerator();  
    110         }
    111
    112         #endregion  
    113  
    114         private AssemblyName CreateAssemblyNameSafe(string assemblyName)  
    115         {  
    116             var result = default(AssemblyName);  
    117             try 
    118             {  
    119                 result = new AssemblyName(assemblyName);  
    120             }  
    121             catch 
    122             {  
    123                 result = null;  
    124             }  
    125             return result;  
    126         }  
    127     } 
    And now you can enumerate all assemblies in the GAC:
    1 static void Main(string[] args)  
    2         {  
    3             foreach(var asm in new GlobalAssemblyCache())  
    4             {  
    5                 Console.WriteLine(asm);  
    6             }  
    7             Console.ReadKey();  
    8         } 

    E' più facile spezzare un atomo che un pregiudizio
    Wednesday, July 23, 2008 1:15 AM
  • Thanks a lot..  Thats exactly what I wanted..
    Wednesday, July 23, 2008 5:30 AM
  • Hi,

    I have installed and uninstalled the assemblies using the GAC API.(i,e) With out using the GacUtil.exe.

    Now i want to remove the particular version of the assemblies.

    Example:

    If GAC contains assemblies like

    Chart.MVC 2.1.0.7

    Chart.MVC 2.1.0.8

    If i passed the /u option with the full qualified name it will remove only one assembly. But i need to remove all the assemblies with the same base version. I don't want to consider the last digit of the assembly version that is Build Number.

    Ex Fully qualified name  ----> Name:Chart.MVC, Culture:Neutral, Version:2.1.0 Processor Architecture:MSIL

    There is one option in the Gacutil.exe /l which returns the list of assemblies.

    My question is how can list the assemblies using the GAC API.

    There is one option to list assemblies from the GAC using the shfusion.dll. But i don't know how to use it.

    Please can any one assist me on this ?


    Regards,

    Velusamy

    Friday, March 26, 2010 3:04 PM
  • You need IAssemblyEnum, and this kind of thing:

    http://blogs.msdn.com/junfeng/articles/229649.aspx 


    Phil Wilson
    Friday, March 26, 2010 6:55 PM
  • You need IAssemblyEnum, and this kind of thing:

    http://blogs.msdn.com/junfeng/articles/229649.aspx 


    Phil Wilson
    Friday, March 26, 2010 6:56 PM