简体   繁体   English

获取winforms应用程序名称的正确方法是什么?

[英]what is the right way of getting my winforms application's name?

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? 或者还有其他正确的方法吗?

Thanks. 谢谢。

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 ): 根据您考虑的应用程序名称 ,甚至还有第三种选择:获取程序集标题或产品名称(通常在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);
}

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. Application.ExecutablePath返回启动应用程序的可执行文件的路径,包括可执行文件名,这意味着如果有人重命名该文件,则值会更改。

Assembly.GetEntryAssembly().GetName().Name returns the simple name of the assembly. Assembly.GetEntryAssembly().GetName().Name返回程序集的简单名称。 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. 因此,GetName()。Name似乎更加可敬。

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. 我假设ExecutablePath比GetName()更快,因为在GetName()中需要Reflection,但是应该测量它。

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. 尝试构建此控制台应用程序,运行它,然后尝试使用Windows文件资源管理器重命名可执行文件名,直接双击重命名的可执行文件再次运行。
The ExecutablePath reflects the change, the Assembly name is still the same ExecutablePath反映了更改,程序集名称仍然相同

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行时调整WinForms应用程序主窗体的起始大小和位置的最安全方法是什么? - What's the safest way to adjust my WinForms application main form starting size and position at run time? 在.NET中命名电子邮件附件的正确方法是什么? - What's the right way to name an email attachment in .NET? 关闭.Net Winforms应用程序并删除可执行文件的最佳方法是什么 - What's the best way to shut down a .Net winforms application and delete the executable .NET winforms应用程序在不使用ClickOnce的情况下更新自身的最佳方法是什么? - What's the best way for a .NET winforms application to update itself without using ClickOnce? 在WinForms应用程序中获取DLL - Getting DLLs in WinForms application 让 WinForms 应用程序永久运行的最佳方式是什么 - What is the best way to have a WinForms application running eternally 在一段时间后以编程方式关闭WinForms应用程序的正确方法是什么? - What is the proper way to programmatically close a WinForms application after a certain time? WinForms应用程序中绘制数千条线的最快方法是什么 - What is the fastest way to draw thousands of lines in WinForms application 编写数据访问层方法的正确方法是什么? - What's the right way to write my data access layer's methods? Winforms:每当在我的应用程序中打开表单时,是否有一种通知方法? - Winforms: Is there a way to be informed whenever a form gets opened in my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM