What is the right way of getting my WinForms application's name?
Asked Answered
S

3

9

I could do this

return Assembly.GetEntryAssembly().GetName().Name;

or

return Path.GetFileNameWithoutExtension(Application.ExecutablePath);

Would both give the desired application name always? If so which is a more standard way of getting application name? If its still a no-win situation is there anything like one method is faster than the other? Or else is there any other right approach?

Sienna answered 3/3, 2012 at 10:54 Comment(2)
Hard to guess what you mean by "application name", the context matters. Both will return the EXE file name. Same thing as the process name.Hinkel
@HansPassant oh, I mean the main name of my product. For example for MS Word, it is "Microsoft Word" and not "WINWORD". In my case, that name which I have hardcoded in Application Properties as Assembly Name. What would be the approach to get that?Sienna
I
11

Take a look at Application.ProductName and Application.ProductVersion

Irick answered 3/3, 2012 at 11:18 Comment(0)
P
4

Depending on what you're considering to be the application name, there's even a third option: get the assembly title or product name (those are usually declared in AssemblyInfo.cs):

object[] titleAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true);
if (titleAttributes.Length > 0 && titleAttributes[0] is AssemblyTitleAttribute)
{
    string assemblyTitle = (titleAttributes[0] as AssemblyTitleAttribute).Title;
    MessageBox.Show(assemblyTitle);
}

or:

object[] productAttributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
if (productAttributes.Length > 0 && productAttributes[0] is AssemblyProductAttribute)
{
    string productName = (productAttributes[0] as AssemblyProductAttribute).Product;
    MessageBox.Show(productName);
}
Passade answered 3/3, 2012 at 11:21 Comment(5)
why is titleAttributes[0] is AssemblyTitleAttribute checking done here? We have already queried the same thing right?Sienna
@nawfal: Yes, you're right, it's highly unlikely that the items in the array will be of any other type, but certain static code analysis tools (like ReSharper) will mark this code as a potential NullReferenceException threat because obj as T expression will evaluate to null if obj is not T. By writing obj is T before obj as T, these tools will know at compile-time that no exceptions will be thrown.Passade
Yes I get that. While certainly I employed your code for getting application title, I think Application.ProductName was the shortest and easiest given the question.Sienna
@nawfal: Yes, you did the right thing, Application.ProductName is a good choice for a WinForms application. Querying the attributes can help if the application is not Windows Forms. To be completely precise, the internal implementation of Application.ProductName retrieves the AssemblyProductAttribute value and caches it.Passade
Application.ProductName and Application.ProductVersion applies to NET Core 3.1. And Assembly and MessageBox.Show ?Schwejda
E
1

It depends how you define 'application name'.

Application.ExecutablePath returns the path for the executable file that started the application, including the executable name, this means that if someone rename the file the value changes.

Assembly.GetEntryAssembly().GetName().Name returns the simple name of the assembly. This is usually, but not necessarily, the file name of the manifest file of the assembly, minus its extension

So, the GetName().Name seem more affidable.

For the faster one, I don't know. I presume that ExecutablePath is faster than GetName() because in the GetName() requires Reflection, but this should be measured.

EDIT:

Try to build this console app, run it and then try to rename the executable file name using the Windows File Explorer, run again directly with the double click on the renamed executable.
The ExecutablePath reflects the change, the Assembly name is still the same

using System;
using System.Reflection;
using System.Windows.Forms;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Assembly.GetEntryAssembly().GetName().Name);
            Console.WriteLine(Application.ExecutablePath);
            Console.ReadLine();
        }
    }
}
Extraterrestrial answered 3/3, 2012 at 11:13 Comment(1)
Do you know in which scenario they differ? executable name (from ExecutablePath) seem to be exactly the name of the assembly.Sienna

© 2022 - 2024 — McMap. All rights reserved.