How do I programmatically get the GUID of an appli

2019-01-08 17:55发布

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?

7条回答
虎瘦雄心在
2楼-- · 2019-01-08 18:29

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.
Console.WriteLine(assembly.GetType().GUID.ToString());


// The following is the correct code.
var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute),true)[0];
var id = attribute.Value;
查看更多
来,给爷笑一个
3楼-- · 2019-01-08 18:30

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();
        var attribs = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
        Console.WriteLine((attribs[0] as GuidAttribute).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 (eg - when loading a plugin)

查看更多
Anthone
4楼-- · 2019-01-08 18:31

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);
}
查看更多
祖国的老花朵
5楼-- · 2019-01-08 18:39

In case anyone else is looking 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();

Update:

Since this has gotten a little bit of attention I decided to include another way of doing it I've been using. 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();
        }
    }
查看更多
一夜七次
6楼-- · 2019-01-08 18:42

Or, just as easy:

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

Works for me...

查看更多
我命由我不由天
7楼-- · 2019-01-08 18:43

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;

查看更多
登录 后发表回答