Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.ProductName equivalent in WPF?

Tags:

c#

wpf

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

like image 721
woany Avatar asked Feb 23 '10 19:02

woany


5 Answers

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
like image 21
Duncan McGregor Avatar answered Oct 16 '22 20:10

Duncan McGregor


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

like image 187
itowlson Avatar answered Oct 16 '22 18:10

itowlson


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;
like image 31
luka Avatar answered Oct 16 '22 18:10

luka


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

like image 45
dodgy_coder Avatar answered Oct 16 '22 20:10

dodgy_coder


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

var productName = Assembly.GetEntryAssembly()
    .GetCustomAttributes(typeof(AssemblyProductAttribute))
    .OfType<AssemblyProductAttribute>()
    .FirstOrDefault().Product;
like image 3
Kostiantyn Ko Avatar answered Oct 16 '22 19:10

Kostiantyn Ko