[英]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.