C# Application.ProductName 在 WPF 中等效吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2321150/
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
Application.ProductName equivalent in WPF?
提问by woany
I have a class library that is nested two+ layers under a main GUI application, within that nested class library I want to be able to access the main applications name.
我有一个类库,它嵌套在主 GUI 应用程序下两层以上,在该嵌套类库中,我希望能够访问主应用程序名称。
Under .Net 3.5 you could call Application.ProductName to retrieve the value from the Assembly.cs file, but I cannot identify an equivalent in WPF. If I use reflection and GetExecutingAssembly then it returns the class libraries details?
在 .Net 3.5 下,您可以调用 Application.ProductName 从 Assembly.cs 文件中检索值,但我无法在 WPF 中识别等效项。如果我使用反射和 GetExecutingAssembly 那么它会返回类库的详细信息吗?
Thanks
谢谢
采纳答案by itowlson
You can use Assembly.GetEntryAssembly()
to get the EXE assembly, and can then use Reflection to get the AssemblyProductAttribute from that.
您可以使用Assembly.GetEntryAssembly()
获取 EXE 程序集,然后可以使用反射从中获取 AssemblyProductAttribute。
This assumes that the product name has been set on the EXE assembly. The WinForms Application.ProductName
property actually looked in the assembly containing the main form, so it works even if the GUI is built in a DLL. To replicate this in WPF you would use Application.Current.MainWindow.GetType().Assembly
(and again use Reflection to get the attribute).
这假定产品名称已在 EXE 程序集中设置。WinFormsApplication.ProductName
属性实际上在包含主窗体的程序集中查看,因此即使 GUI 内置在 DLL 中它也能工作。要在 WPF 中复制它,您将使用Application.Current.MainWindow.GetType().Assembly
(并再次使用反射来获取属性)。
回答by Michael James
The answer you require is:
您需要的答案是:
Path.GetFileName(Assembly.GetEntryAssembly().GetName().Name)
回答by Duncan McGregor
Here is another solution that I am using to get the Product Name
这是我用来获取产品名称的另一个解决方案
Public Shared Function ProductName() As String
If Windows.Application.ResourceAssembly Is Nothing Then
Return Nothing
End If
Return Windows.Application.ResourceAssembly.GetName().Name
End Sub
回答by dodgy_coder
If you need to get the descriptive product name as I did, then this solution may be useful:
如果您需要像我一样获得描述性的产品名称,那么此解决方案可能很有用:
// Get the Product Name from the Assembly information
string productName = String.Empty;
var list = Application.Current.MainWindow.GetType().Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), true);
if (list != null)
{
if (list.Length > 0)
{
productName = (list[0] as AssemblyProductAttribute).Product;
}
}
It returns whatever you've set for the 'AssemblyProduct' attribute in the AssemblyInfo.cs file, e.g. something like "Widget Engine Professional".
它返回您为 AssemblyInfo.cs 文件中的“AssemblyProduct”属性设置的任何内容,例如“Widget Engine Professional”之类的内容。
回答by luka
in wpf there are many way to do this , here you can find two of this.
在 wpf 中有很多方法可以做到这一点,在这里你可以找到其中的两种。
using System;`
using System.Windows;
String applicationName = String.Empty;
//one way
applicationName = AppDomain.CurrentDomain.FriendlyName.Split('.')[0];
//other way
applicationName = Application.ResourceAssembly.GetName().Name;
回答by Kostiantyn Kolesnichenko
Based on the answers above, this works just great immediately:
根据上面的答案,这立即生效:
var productName = Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyProductAttribute))
.OfType<AssemblyProductAttribute>()
.FirstOrDefault().Product;
回答by pr0gg3r
If you are looking for the values provided by the assembly information, e.g. the title...
如果您正在寻找程序集信息提供的值,例如标题...
... then you have to get the custom attributes like this:
...然后你必须获得这样的自定义属性:
using System.Linq;
using System.Reflection;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Title = (Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute)).SingleOrDefault() as AssemblyTitleAttribute)?.Title;
}
}
}