20

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.

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?

Thanks

0

7 Answers 7

34

You can use Assembly.GetEntryAssembly() to get the EXE assembly, and can then use Reflection to get the AssemblyProductAttribute from that.

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).

4 Comments

Many thanks for your quick reply, have added the following and it works like a charm: Application.Current.MainWindow.GetType().Assembly.GetName().Name
Note that GetEntryAssembly() could return null under certain circumstances.
Here again after a while.
Application.Current.MainWindow can be null too. For instance, I needed to get product name before the main window of a WPF app was created. @Kostiantyn Kolesnichenko's solution worked for me in this case.
8

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

1 Comment

The question is for C#, not VB
7

in wpf there are many way to do this , here you can find two of this.

using System;`
using System.Windows;
String applicationName = String.Empty;

//one way
applicationName = AppDomain.CurrentDomain.FriendlyName.Split('.')[0];

 //other way
applicationName = Application.ResourceAssembly.GetName().Name;

Comments

5

Based on the answers above, this works just great immediately:

var productName = Assembly.GetEntryAssembly()
    .GetCustomAttributes(typeof(AssemblyProductAttribute))
    .OfType<AssemblyProductAttribute>()
    .FirstOrDefault().Product;

1 Comment

Yes, thanks, works well when, in my case, the mainwindow is not created yet.
4

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".

Comments

3

If you are looking for the values provided by the assembly information, e.g. the title...

enter image description here

... 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;
        }
    }
}

enter image description here

Comments

2

The answer you require is:

Path.GetFileName(Assembly.GetEntryAssembly().GetName().Name)

2 Comments

So, basically, your complaint is that "far too many people" do more than simply giving you the codez? I disagree; there's a lot of benefit in explanation. Far too many people copy and paste code they find online into their project. That's a much worse phenomenon.
Assembly name is different from product name. Application.Product name gives you the AssemblyProduct attribute set for the assembly. Assembly name is the name of the assembly, ie that physical name in file system it compiles to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.