2013-01-20 18 views
0

我需要将Windows 8应用程序名称变为一个变量,但我找不到一种方法来执行此操作。如何以编程方式从属性获取我的Windows 8应用程序名称?

我想从应用程序属性获得标题(“TEMEL UYGULAMA”),如本屏幕截图:http://prntscr.com/psd6w

或者如果有谁知道反正让应用程序的名称或标题,我可以用它。我只需要获取应用程序名称或标题(在程序集内)

感谢您的帮助。

回答

0

从您的屏幕截图看来,您似乎想要汇编标题。你可以做这样的事情在运行时获取大会title属性:

// Get current assembly 
var thisAssembly = this.GetType().Assembly; 

// Get title attribute (on .NET 4) 
var titleAttribute = thisAssembly 
     .GetCustomAttributes(typeof(AssemblyTitleAttribute), false) 
     .Cast<AssemblyTitleAttribute>() 
     .FirstOrDefault(); 

// Get title attribute (on .NET 4.5) 
var titleAttribute = thisAssembly.GetCustomAttribute<AssemblyTitleAttribute>(); 

if (titleAttribute != null) 
{ 
    var title = titleAttribute.Title; 
    // Do something with title... 
} 

但请记住,这是不应用程序的名称,这是大会冠军。

+0

谢谢,但在您的示例代码中存在一个问题:“'System.Type'不包含'Assembly'的定义,并且没有扩展方法'Assembly'接受类型的第一个参数可以找到'System.Type'(你是否缺少使用指令或程序集引用?)“ 你也有解决方案吗?谢谢。 –

+0

嗯,这很奇怪,我知道'System.Type'有一个名为'Assembly'的属性。只需查看[documentation](http://msdn.microsoft.com/en-us/library/windows/apps/system.type.aspx)。如果你不能得到它的工作,你可以尝试'this.GetType()。GetTypeInfo()。Assembly',看看是否可以帮助你,但这个属性是从'System.Type'继承的,所以我不看看为什么它应该有所作为。 – khellang

+0

如果您想尝试使用'this.GetType()。GetTypeInfo()。Assembly',您可能需要在文件中添加'using System.Reflection;'。总有其他方法可以获得你想要的'Assembly'。你可以尝试'typeof()。Assembly或者'Assembly.GetExecutingAssembly()'... – khellang

0

我用像这样的代码来获得我的的Windows Store应用组装Title属性

首先,你需要这些组件:

using System.Reflection; 
using System.Linq; 

...然后代码像这应该工作(可能更多的检查):

// Get the assembly with Reflection: 
Assembly assembly = typeof(App).GetTypeInfo().Assembly; 

// Get the custom attribute informations: 
var titleAttribute = assembly.CustomAttributes.Where(ca => ca.AttributeType == typeof(AssemblyTitleAttribute)).FirstOrDefault(); 

// Now get the string value contained in the constructor: 
return titleAttribute.ConstructorArguments[0].Value.ToString(); 

希望这有助于...

相关问题