What is the correct way to get my winforms application name?

I could do this:

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

or

return Path.GetFileNameWithoutExtension(Application.ExecutablePath);

Both will give the desired application name always ? If so, is this a more standard way to get the application name? If there is still no victory situation in it, is there something like one method faster than another? Or is there a different approach?

Thank.

+5
source share
3 answers

, , : ( 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);
}

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);
}
+4

, " ".

Application.ExecutablePath , , , , - , .

Assembly.GetEntryAssembly().GetName().Name . , , ,

, GetName(). .

, . , ExecutablePath , GetName(), GetName() Reflection, .

EDIT:

Try to create this console application, run it, and then try to rename the executable file name using Windows File Explorer, run it again by double-clicking on the renamed executable file. ExecutablePath reflects the change, the assembly name is the same as

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();
        }
    }
}
+1
source

All Articles