120

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

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nathan
  • 11,938
  • 12
  • 55
  • 62
  • 26
    This question has an interesting history: https://i.imgur.com/uOY8lDF.jpeg – Pikamander2 Aug 22 '21 at 09:28
  • 8
    For anyone that doesn't find an image useful, see https://twitter.com/foone/status/1229641258370355200?lang=en re: the old answer with a bug that was copy-pasted into a few things, causing them to mutually-exclude *each other* instead of just other instances of themselves. – Peter Cordes Nov 03 '21 at 07:32
  • 1
    A now-deleted answer was referenced in [the Twitter thread](https://twitter.com/Foone/status/1229643844515287047). – Peter Mortensen Jul 31 '22 at 11:22

8 Answers8

172

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;
    Console.WriteLine(id);
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 4
    how about using 'AppDomain.CurrentDomain.DomainManager.EntryAssembly' instead of 'typeof(Program).Assembly'? well, we could change Program class's name, can't we? – Kenial Oct 22 '12 at 17:40
  • 1
    @Kenial I get `System.AppDomain.DomainManager.get returned null.` for a simple console app. Seems like `Assembly.GetEntryAssembly()` is the preferred way. – Jesse C. Slicer Feb 25 '20 at 16:55
14

Or, just as easy:

string assyGuid = Assembly.GetExecutingAssembly().GetCustomAttribute<GuidAttribute>().Value.ToUpper();

It works for me...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
11

Another way is to use Marshal.GetTypeLibGuidForAssembly.

According to MSDN:

When assemblies are exported to type libraries, the type library is assigned a LIBID. You can set the LIBID explicitly by applying the System.Runtime.InteropServices.GuidAttribute at the assembly level, or it can be generated automatically. The Tlbimp.exe (Type Library Importer) tool calculates a LIBID value based on the identity of the assembly. GetTypeLibGuid returns the LIBID that is associated with the GuidAttribute, if the attribute is applied. Otherwise, GetTypeLibGuidForAssembly returns the calculated value. Alternatively, you can use the GetTypeLibGuid method to extract the actual LIBID from an existing type library.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andrey.ko
  • 852
  • 1
  • 7
  • 17
  • I was able to do this in F#, it seems that the assembly doesn't have a custom attribute with the Guid – fableal Sep 18 '13 at 20:32
  • This method works even with `Assembly.ReflectionOnlyLoad` and even when dependent assemblies are not loaded. – Martin Lottering Aug 13 '15 at 07:08
  • So, the full code is: `System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Reflection.Assembly.GetExecutingAssembly()).ToString()`. Looks much simpler than the other method. Are there any downsides? – user Mar 08 '16 at 20:50
8

You should be able to read the GUID attribute of the assembly via reflection. This will get the GUID for the current assembly:

Assembly asm = Assembly.GetExecutingAssembly();
object[] attribs = asm.GetCustomAttributes(typeof(GuidAttribute), true);
var guidAttr = (GuidAttribute) attribs[0];
Console.WriteLine(guidAttr.Value);

You can replace the GuidAttribute with other attributes as well, if you want to read things like AssemblyTitle, AssemblyVersion, etc.

You can also load another assembly (Assembly.LoadFrom and all) instead of getting the current assembly - if you need to read these attributes of external assemblies (for example, when loading a plugin).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amazedsaint
  • 7,642
  • 7
  • 54
  • 83
  • 8
    Please don't use "as" casts if you are going to use the result of the cast no matter what. It is generally bad style because you get a NullReferenceException instead of the more informative InvalidCastException if the cast fails. "as" casts are for when you don't know whether the object is of the given type and only want to use it in case it is. Just use a direct ((GuidAttribute)attribs[0]).Value instead if you don't expect it to be of another type (which it shouldn't) – poizan42 Aug 17 '15 at 23:15
7

For an out-of-the-box working example, this is what I ended up using based on the previous answers.

using System.Reflection;
using System.Runtime.InteropServices;

label1.Text = "GUID: " + ((GuidAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute), false)).Value.ToUpper();

Alternatively, this way allows you to use it from a static class:

/// <summary>
/// public GUID property for use in static class </summary>
/// <returns>
/// Returns the application GUID or "" if unable to get it. </returns>
static public string AssemblyGuid
{
    get
    {
        object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(GuidAttribute), false);
        if (attributes.Length == 0) { return String.Empty; }
        return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value.ToUpper();
    }
}
Teocci
  • 7,189
  • 1
  • 50
  • 48
Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
1

There wasn't any luck here with the other answers, but I managed to work it out with this nice one-liner:

((GuidAttribute)(AppDomain.CurrentDomain.DomainManager.EntryAssembly).GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tkefauver
  • 491
  • 5
  • 19
0

To get the appID you could use the following line of code:

var applicationId = ((GuidAttribute)typeof(Program).Assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;

For this you need to include the System.Runtime.InteropServices;

drgmak
  • 1,135
  • 10
  • 13
0

Use:

 string AssemblyID = Assembly.GetEntryAssembly().GetCustomAttribute<GuidAttribute>().Value;

Or in VB.NET:

  Dim AssemblyID As String = Assembly.GetEntryAssembly.GetCustomAttribute(Of GuidAttribute).Value
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SophieT
  • 21
  • 4