【问题标题】:How do I programmatically get the GUID of an application in .NET 2.0如何以编程方式获取 .NET 2.0 中应用程序的 GUID
【发布时间】:2010-10-04 21:02:33
【问题描述】:

我需要在 C# .NET 2.0 中访问我的项目的程序集。

我可以在项目属性下的“装配信息”对话框中看到 GUID,目前我刚刚将其复制到代码中的 const 中。 GUID 永远不会改变,所以这不是一个糟糕的解决方案,但直接访问它会很好。有没有办法做到这一点?

【问题讨论】:

标签: c# .net visual-studio-2005 .net-2.0 guid


【解决方案1】:

您应该能够通过反射读取程序集的 GUID 属性。这将获得当前程序集的 GUID

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

如果您想阅读 AssemblyTitle、AssemblyVersion 等内容,您也可以将 GuidAttribute 替换为其他属性。

如果您需要读取外部程序集的这些属性(例如,加载插件时),您还可以加载另一个程序集(Assembly.LoadFrom 和所有程序集)而不是获取当前程序集。

【讨论】:

  • 如果你无论如何都要使用转换的结果,请不要使用“as”转换。这通常是不好的风格,因为如果转换失败,您会得到 NullReferenceException 而不是更多信息的 InvalidCastException。当您不知道对象是否属于给定类型并且只想在它是给定类型时使用它时,“as”强制转换适用。如果您不希望它是另一种类型(它不应该),请使用直接 ((GuidAttribute)attribs[0]).Value 代替
【解决方案2】:

试试下面的代码。您要查找的值存储在附加到程序集的 GuidAttribute 实例中

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);
}

【讨论】:

  • 使用 'AppDomain.CurrentDomain.DomainManager.EntryAssembly' 而不是 'typeof(Program).Assembly' 怎么样?好吧,我们可以更改 Program 类的名称,不是吗?
  • @Kenial 我得到了System.AppDomain.DomainManager.get returned null. 一个简单的控制台应用程序。似乎Assembly.GetEntryAssembly() 是首选方式。
【解决方案3】:

另一种方法是使用Marshal.GetTypeLibGuidForAssembly

根据MSDN

将程序集导出到类型库时,会为类型库分配一个 LIBID。您可以通过在程序集级别应用 System.Runtime.InteropServices.GuidAttribute 来显式设置 LIBID,也可以自动生成它。 Tlbimp.exe(类型库导入器)工具根据程序集的标识计算 LIBID 值。 GetTypeLibGuid 返回与 GuidAttribute 关联的 LIBID(如果应用了该属性)。否则,GetTypeLibGuidForAssembly 返回计算值。或者,您可以使用 GetTypeLibGuid 方法从现有类型库中提取实际的 LIBID。

【讨论】:

  • 我可以在 F# 中做到这一点,似乎程序集没有带有 Guid 的自定义属性
  • 此方法适用于Assembly.ReflectionOnlyLoad,甚至在未加载依赖程序集的情况下。
  • 所以,完整的代码是:System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Reflection.Assembly.GetExecutingAssembly()).ToString()。看起来比其他方法简单得多。有什么缺点吗?
【解决方案4】:

对于一个开箱即用的工作示例,这是我根据之前的答案最终使用的。

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

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

或者,这种方式允许您从静态类中使用它:

    /// <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();
        }
    }

【讨论】:

    【解决方案5】:

    要获取 appID,您可以使用以下代码行:

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

    为此,您需要包含System.Runtime.InteropServices;

    【讨论】:

      【解决方案6】:

      或者,同样简单:

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

      它对我有用...

      【讨论】:

        【解决方案7】:

        其他答案在这里没有任何运气,但我设法用这个漂亮的单线解决了这个问题:

        ((GuidAttribute)(AppDomain.CurrentDomain.DomainManager.EntryAssembly).GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value
        

        【讨论】:

          【解决方案8】:
           string AssemblyID = Assembly.GetEntryAssembly().GetCustomAttribute<GuidAttribute>().Value;
          

          或者,VB.NET:

            Dim AssemblyID As String = Assembly.GetEntryAssembly.GetCustomAttribute(Of GuidAttribute).Value
          

          【讨论】:

            猜你喜欢
            • 2011-02-24
            • 2010-10-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-13
            • 1970-01-01
            • 2016-06-26
            • 1970-01-01
            相关资源
            最近更新 更多