locked
get GUID from Assembly Information RRS feed

  • Question

  • I want to know how can I retrieve the GUID stored in the Assembly Information in the project properties using C#?

    Jassim Rahma
    Wednesday, September 23, 2009 6:55 AM

Answers

All replies

  • 
    
    System.Reflection.Assembly thisAssembly = this.GetType().Assembly;
    object[] attributes = thisAssembly.GetCustomAttributes 
       (typeof(GuidAttribute), false));
    if (attributes.Length == 1)
    {
       Console.WriteLine (((AssemblyTitleAttribute) attributes[0]).Title);
    }
    
    

    Wednesday, September 23, 2009 8:22 AM
  • var attrs = Assembly.GetExecutingAssembly().GetCustomAttributes(false).OfType<System.Runtime.InteropServices.GuidAttribute>();
    if (attrs.Any()) {
        Console.WriteLine(attrs.First().Value);
    }

    See http://c-sharp-programming.blogspot.com/2008/08/how-to-get-c-assembly-guid.html
    • Proposed as answer by Jiyuan Wednesday, September 23, 2009 12:54 PM
    • Marked as answer by eryang Monday, September 28, 2009 6:20 AM
    Wednesday, September 23, 2009 9:19 AM
  •        public String GetCurrentAssemblyGuid()
            {
                System.Reflection.Assembly assembly = this.GetType().Assembly;
                Object[] attributes = assembly.GetCustomAttributes
                   (typeof(GuidAttribute), false);
                if (attributes.Any())
                {
                    return ((GuidAttribute)attributes.First()).Value;
                }
                return "";
            }
    Wednesday, September 23, 2009 12:53 PM
  • Hi Rahma,
    We can also get the GUID attribute of arbitrary assemblies by using Assembly.LoadXX() or Assembly.ReflectionOnlyLoadXX() methods, since the assemblies are loaded as Assembly, there are some limitations:
    1. We cannot load a 64-bit assembly into a 32-bit managed application.
    2. The assembly load/unload overhead is tend to be considered if our application is performance-sensitive.
    3. Sometime, assembly dependency will make trouble to us when loading an assembly.

    So, as backup solution, we can load assembly file as PE file:
    1. Cci provides the ability to acccess to everything in a CLR PE file.
    2. This book shows more detail on how to read PE file.

    Anyway, load assembly as PE file is always not the first choice, we'd better use those methods given by Assembly type.

    See also: Assembly.Load vs Assembly.LoadFrom vs Assembly.LoadFile
    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/3bdaf65c-520c-4a1a-a825-fc2ca2957bf3
    http://sendhil.spaces.live.com/blog/cns!30862CF919BD131A!578.entry
    http://blogs.msdn.com/suzcook/archive/2003/09/19/loadfile-vs-loadfrom.aspx

    Thanks,
    Eric


    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Proposed as answer by eryang Monday, September 28, 2009 6:19 AM
    • Marked as answer by eryang Monday, September 28, 2009 6:20 AM
    Thursday, September 24, 2009 10:03 AM
  • I have added the following code:

                var attrs = Assembly.GetExecutingAssembly().GetCustomAttributes(false).OfType<System.Runtime.InteropServices.GuidAttribute>();
                if (attrs.Any())
                {
                    Console.WriteLine(attrs.First().Value);
                }
    


    but getting this error:
    Error	1
    'System.Array' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of
    type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
    C:\Users\Jassim\Documents\Visual Studio 2008\Projects\Internet Cafe\Internet Cafe\main_form.cs	374 84 
    
    



    Jassim Rahma
    Friday, October 16, 2009 10:38 PM
  • 1. Make sure you have a reference to System.Core.dll
    2. add a using directive to import the System.Linq namespace ie "using System.Linq;"
    Saturday, October 17, 2009 7:16 PM
  • Hi, I have try this:

     

     

    public string GuidValue {
    get {
      GuidAttribute GuidAttribute = (GuidAttribute)GuidAttribute.GetCustomAttribute(this.GetType().Assembly, typeof(GuidAttribute));
      return GuidAttribute.Value;
      }
    }

    Bye

    Thursday, August 5, 2010 10:57 AM