我想在我的 Microsoft Visual C# 2008 Express Edition 项目的程序集信息中使用 GUID,以在 Mutex 中使用,该 Mutex 用于验证只有一个应用程序实例正在运行。如何访问 GUID?我只需要在Program.Main(...)
函数中使用它。谢谢。
问问题
6258 次
2 回答
4
MSDN 上也有人问过类似的问题:
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/5faa901a-7e70-41f4-a096-3e6053afd5ec
这是代码摘录:
public static T GetAssemblyAttribute<T>(Assembly assembly) where T : Attribute
{
if (assembly == null) return null;
object[] attributes = assembly.GetCustomAttributes(typeof(T), true);
if (attributes == null) return null;
if (attributes.Length == 0) return null;
return (T)attributes[0];
}
一些值也可以通过以下Assembly
类型获得:
于 2010-07-28T19:32:04.130 回答
1
您可以在 Assembly 上调用静态GetExecutingAssembly 方法来获取当前正在执行的程序集。
但是,除非您具有与程序集关联的Guid 属性,否则不能保证始终具有与程序集关联的 Guid。
如果确实有与程序集关联的 Guid 属性,则可以获取程序集实例并对其调用GetCustomAttributes,它将返回与程序集关联的 Guid 属性,然后您可以查询该 Guid。
但是,我建议使用应用程序入口点的完全限定类型名称作为名称。它具有更好的语义含义,并且具有组装资格,因此具有相当大的独特性。
如果您担心其他人使用该值来阻止您的应用程序运行,那么使用 Guid 并没有那么安全,有人总是可以反映您的程序集并获取 Guid 的值。
于 2010-07-28T19:32:30.243 回答