8

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?

Vy Do
  • 46,709
  • 59
  • 215
  • 313
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 3
    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. – Hans Passant Mar 03 '12 at 11:30
  • 1
    @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? – nawfal Mar 03 '12 at 11:34

3 Answers3

11

Take a look at Application.ProductName and Application.ProductVersion

Nikola Markovinović
  • 18,963
  • 5
  • 46
  • 51
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);
}
Yuriy Guts
  • 2,180
  • 1
  • 14
  • 18
  • why is `titleAttributes[0] is AssemblyTitleAttribute` checking done here? We have already queried the same thing right? – nawfal Mar 03 '12 at 14:37
  • @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. – Yuriy Guts Mar 03 '12 at 15:04
  • 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. – nawfal Mar 03 '12 at 15:06
  • @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. – Yuriy Guts Mar 03 '12 at 15:10
  • `Application.ProductName and Application.ProductVersion` applies to **NET Core 3.1**. And `Assembly and MessageBox.Show` ? – Kiquenet Dec 10 '20 at 12:08
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();
        }
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Do you know in which scenario they differ? executable name (from `ExecutablePath`) seem to be exactly the name of the assembly. – nawfal Jan 08 '14 at 15:59