如何编程得到.net2.0的应用程序的GUID [英] How do I programmatically get the GUID of an application in .net2.0

查看:186
本文介绍了如何编程得到.net2.0的应用程序的GUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问我的项目的组件在C#.NET2.0。

I need to access the assembly of my project in C# .NET2.0.

我可以看到的GUID的程序集信息对话框中根据项目属性,并在那一刻我刚刚把它复制到在code常量。该GUID永远不会改变,所以这不是一个坏的解决方案,但它会是不错的直接访问。有没有办法做到这一点?

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?

推荐答案

编辑:对于那些谁坚持downvoting ...无法删除这个答案,因为它是公认的版本。因此,我的编辑,包括正确答案( JaredPar的code以下

To those who insist on downvoting... Unable to delete this answer because it is the accepted version. Therefore, am editing to include the correct answer (JaredPar's code below)

不够的,如果你只想得到执行装配简单:

Simple enough if you only want to get the Executing assembly:

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;

这篇关于如何编程得到.net2.0的应用程序的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆