如何以编程方式获取.net2.0中的应用程序的GUID

问题描述 投票:40回答:7

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

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

c# .net visual-studio-2005 .net-2.0 guid
7个回答
61
投票

请尝试以下代码。您要查找的值存储在附加到Assembly的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);
}

32
投票

编辑:对那些坚持downvoting的人...无法删除此答案,因为它是可接受的版本。因此,我正在编辑以包含正确的答案(下面是JaredPar's code

如果您只想获得执行程序集,那么很简单:

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;

9
投票

另一种方法是使用Marshal.GetTypeLibGuidForAssembly

根据msdn:

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


6
投票

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

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

如果你想阅读AssemblyTitle,AssemblyVersion等内容,你也可以用其他属性替换GuidAttribute。

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


5
投票

如果其他人正在寻找一个开箱即用的工作示例,这是我最终使用的基于以前的答案。

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

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

Update:

由于这引起了一点点关注,我决定采用另一种方式来实现,我一直在使用它。这种方式允许您从静态类中使用它:

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

1
投票

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

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

为此你需要包括System.Runtime.InteropServices;


1
投票

或者,同样简单:

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

对我有用......

© www.soinside.com 2019 - 2024. All rights reserved.