How do I programmatically get the GUID of an application in .net2.0


I need to access the assembly of my project in C# .NET2.0.

I can see the GUID in the 'Assembly Information' dialog in under project properties, and at the moment I have just copied it to a const in the code. The GUID will never change, so this is not that bad of a solution, but it would be nice to access it directly. Is there a way to do this?

3/22/2009 10:07:54 PM

Accepted Answer

Try the following code. The value you are looking for is stored on a GuidAttribute instance attached to the Assembly

using System.Runtime.InteropServices;

static void Main(string[] args)
    var assembly = typeof(Program).Assembly;
    var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute),true)[0];
    var id = attribute.Value;
7/8/2016 9:34:43 AM

Edit: To those who insist on downvoting... Unable to delete this answer because it is the accepted version. Therefore, am editing to include the correct answer (JaredPar's code below)

Simple enough if you only want to get the Executing assembly:

using System.Reflection;

Assembly assembly = Assembly.GetExecutingAssembly();

//The following line (part of the original answer) is misleading.
//**Do not** use it unless you want to return the System.Reflection.Assembly type's GUID.

// The following is the correct code.
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute),true)[0];
var id = attribute.Value;

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow