WPF中的Application.ProductName等价物?

woa*_*any 19 c# wpf

我有一个类库,在主GUI应用程序下嵌套两个+层,在该嵌套类库中我希望能够访问主应用程序名称.

在.Net 3.5下,您可以调用Application.ProductName来从Assembly.cs文件中检索值,但我无法识别WPF中的等效项.如果我使用反射和GetExecutingAssembly然后它返回类库详细信息?

谢谢

ito*_*son 31

您可以使用Assembly.GetEntryAssembly()获取EXE程序集,然后可以使用Reflection从中获取AssemblyProductAttribute.

这假设已在EXE程序集上设置了产品名称.WinForms Application.ProductName属性实际上在包含主窗体的程序集中查找,因此即使GUI是在DLL中构建的,它也可以工作.要在WPF中复制它,您将使用Application.Current.MainWindow.GetType().Assembly(并再次使用Reflection来获取属性).

  • 非常感谢您的快速回复,添加了以下内容,它就像一个魅力:Application.Current.MainWindow.GetType().Assembly.GetName().Name (11认同)

小智 7

这是我用来获取产品名称的另一个解决方案

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
Run Code Online (Sandbox Code Playgroud)


luk*_*uka 5

在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;
Run Code Online (Sandbox Code Playgroud)