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

前端 未结 7 1072
囚心锁ツ
囚心锁ツ 2020-11-29 18:52

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

相关标签:
7条回答
  • 2020-11-29 19:12

    Or, just as easy:

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

    Works for me...

    0 讨论(0)
  • 2020-11-29 19:14

    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);
    }
    
    0 讨论(0)
  • 2020-11-29 19:16

    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.

    0 讨论(0)
  • 2020-11-29 19:20

    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;

    0 讨论(0)
  • 2020-11-29 19:34

    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();
            }
        }
    
    0 讨论(0)
  • 2020-11-29 19:36

    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)

    0 讨论(0)
提交回复
热议问题