C# 如何以编程方式获取 .net2.0 中应用程序的 GUID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/502303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I programmatically get the GUID of an application in .net2.0
提问by Nathan
I need to access the assembly of my project in C# .NET2.0.
我需要在 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?
我可以在项目属性下的“程序集信息”对话框中看到 GUID,目前我刚刚将其复制到代码中的常量中。GUID 永远不会改变,所以这不是一个糟糕的解决方案,但直接访问它会很好。有没有办法做到这一点?
采纳答案by JaredPar
Try the following code. The value you are looking for is stored on a GuidAttribute instance attached to the 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);
}
回答by amazedsaint
You should be able to read the Guid attribute of the assembly via reflection. This will get the GUID for the current assembly
您应该能够通过反射读取程序集的 Guid 属性。这将获得当前程序集的 GUID
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
如果您想阅读诸如 AssemblyTitle、AssemblyVersion 等内容,您也可以将 GuidAttribute 替换为其他属性
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)
您还可以加载另一个程序集(Assembly.LoadFrom 和所有)而不是获取当前程序集 - 如果您需要读取外部程序集的这些属性(例如 - 加载插件时)
回答by andrey.ko
Another way is to use Marshal.GetTypeLibGuidForAssembly.
另一种方法是使用Marshal.GetTypeLibGuidForAssembly。
According to msdn:
根据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.
当程序集导出到类型库时,类型库会被分配一个 LIBID。您可以通过在程序集级别应用 System.Runtime.InteropServices.GuidAttribute 来显式设置 LIBID,也可以自动生成。Tlbimp.exe(类型库导入程序)工具根据程序集的标识计算 LIBID 值。如果应用了该属性,则 GetTypeLibGuid 返回与 GuidAttribute 关联的 LIBID。否则,GetTypeLibGuidForAssembly 返回计算值。或者,您可以使用 GetTypeLibGuid 方法从现有类型库中提取实际 LIBID。
回答by Scott Solmer
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();
}
}
回答by drgmak
To get the appID you could use the following line of code:
要获取 appID,您可以使用以下代码行:
var applicationId = ((GuidAttribute)typeof(Program).Assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;
For this you need to include the System.Runtime.InteropServices;
为此,您需要包括 System.Runtime.InteropServices;
回答by drgmak
Or, just as easy:
或者,同样简单:
string assyGuid = Assembly.GetExecutingAssembly().GetCustomAttribute<GuidAttribute>().Value.ToUpper();
Works for me...
对我有用...