Get Assembly info of my current C# Application

Answered Get Assembly info of my current C# Application

  • Sunday, March 09, 2008 5:08 PM
     
     

    Hello, developers...

    How can i get the assembly information of my current C# application, for example its GUID?

     

    Thanks,

    Amin Mashayekhi

All Replies

  • Sunday, March 09, 2008 5:20 PM
     
     Answered

    You can get at this by reflecting on the assembly:

     

    Code Snippet

    Guid assemblyGuid = Guid.Empty;

    object[] assemblyObjects =

    System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(

    typeof(System.Runtime.InteropServices.GuidAttribute), true);

    if (assemblyObjects.Length > 0)

    {

    assemblyGuid = new Guid(((System.Runtime.InteropServices.GuidAttribute)assemblyObjects[0]).Value);

    }

     

     

    You can get at other attributes in a similar fashion.

  • Sunday, March 09, 2008 6:36 PM
     
     Answered

    Here's a little helper method that does the same as Dan Rigsby's code:

     

    Code Snippet

    public static T GetAssemblyAttribute<T>(Assembly assembly) where T : Attribute

    {

        if (assembly == null) return null;

        object[] attributes = assembly.GetCustomAttributes(typeof(T), true);

        if (attributes == null) return null;

        if (attributes.Length == 0) return null;

        return (T)attributes[0];

    }

     

  • Sunday, March 09, 2008 6:41 PM
     
     
    I got a set of helper classes like that in a library too.  I wish they would have added more generics support in methods like these.  They are helpful!

     

  • Thursday, April 30, 2009 2:41 PM
     
     
    Thanks for the information above everyone.  I've written a C# AssemblyInfo class to easily retrieve the information...

    http://bunkerhollow.com/blogs/matt/archive/2009/04/29/net-get-assembly-information-title-version-etc.aspx
  • Thursday, October 14, 2010 11:50 AM
     
      Has Code

    Hi All,

    A bit late in the post, but it helped us super. I've removed some of the redundant code in mddubs class (thanks for that mmdubs):

     

    using System;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace Diartis.KLIB.KLIBLibrary
    {
    	public class AssemblyInfoHelper
    	{
    		private static Assembly m_Assembly;
    
    		public AssemblyInfoHelper(Type type)
    		{
    			Assertions.CheckObject("type", type);
    			m_Assembly = Assembly.GetAssembly(type);
    			Assertions.CheckObject("m_Assembly", m_Assembly);
    		}
    
    		private T CustomAttributes<T>()
    			where T : Attribute
    		{
    			object[] customAttributes = m_Assembly.GetCustomAttributes(typeof(T), false);
    
    			if ((customAttributes != null) && (customAttributes.Length > 0))
    			{
    				return ((T)customAttributes[0]);
    			}
    
    			throw new InvalidOperationException();
    		}
    
    		public string Title
    		{
    			get
    			{
    				return CustomAttributes<AssemblyTitleAttribute>().Title;
    			}
    		}
    
    		public string Description
    		{
    			get
    			{
    				return CustomAttributes<AssemblyDescriptionAttribute>().Description;
    			}
    		}
    
    		public string Company
    		{
    			get
    			{
    				return CustomAttributes<AssemblyCompanyAttribute>().Company;
    			}
    		}
    
    		public string Product
    		{
    			get
    			{
    				return CustomAttributes<AssemblyProductAttribute>().Product;
    			}
    		}
    
    		public string Copyright
    		{
    			get
    			{
    				return CustomAttributes<AssemblyCopyrightAttribute>().Copyright;
    			}
    		}
    
    		public string Trademark
    		{
    			get
    			{
    				return CustomAttributes<AssemblyTrademarkAttribute>().Trademark;
    			}
    		}
    
    		public string AssemblyVersion
    		{
    			get
    			{
    				return m_Assembly.GetName().Version.ToString();
    			}
    		}
    
    		public string FileVersion
    		{
    			get
    			{
    				FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(m_Assembly.Location);
    				return fvi.FileVersion;
    			}
    		}
    
    		public string Guid
    		{
    			get
    			{
    				return CustomAttributes<System.Runtime.InteropServices.GuidAttribute>().Value;
    			}
    		}
    
    		public string FileName
    		{
    			get
    			{
    				FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(m_Assembly.Location);
    				return fvi.OriginalFilename;
    			}
    		}
    
    		public string FilePath
    		{
    			get
    			{
    				FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(m_Assembly.Location);
    				return fvi.FileName;
    			}
    		}
    	}
    }
    
    
    

    Take a look at my blog: http://paul.sinnema.ch
  • Tuesday, October 25, 2011 6:21 PM
     
     

    Paul - where are you getting "Assertions" from?

     

    Thanks (and sorry for beating a dead thread)

  • Tuesday, January 17, 2012 3:10 AM
     
     
  • Tuesday, February 21, 2012 11:56 PM
     
     Proposed

    Or check out Comment #4 at this thread: Reading AssemblyInfo (Microsoft Visual C#)

    hth,
    Wolfgang

    • Proposed As Answer by WolfgangSchober Tuesday, February 21, 2012 11:56 PM
    •