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

前端 未结 7 1120
囚心锁ツ
囚心锁ツ 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: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:

        /// 
        /// public GUID property for use in static class 
        ///  
        /// Returns the application GUID or "" if unable to get it. 
        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();
            }
        }
    

提交回复
热议问题