[英]What's the safest way to adjust my WinForms application main form starting size and position at run time?
[英]what is the right way of getting my winforms application's name?
我能做到这一点:
return Assembly.GetEntryAssembly().GetName().Name;
要么
return Path.GetFileNameWithoutExtension(Application.ExecutablePath);
既能总是得到期望的应用程序的名称? 如果是这样,这是获取应用程序名称的更标准方法? 如果它仍然是一个不赢的局面,有什么比一种方法更快的速度吗? 或者还有其他正确的方法吗?
谢谢。
根据您考虑的应用程序名称 ,甚至还有第三种选择:获取程序集标题或产品名称(通常在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);
}
这取决于您如何定义“应用程序名称”。
Application.ExecutablePath
返回启动应用程序的可执行文件的路径,包括可执行文件名,这意味着如果有人重命名该文件,则值会更改。
Assembly.GetEntryAssembly().GetName().Name
返回程序集的简单名称。 这通常(但不一定)是程序集清单文件的文件名减去其扩展名
因此,GetName()。Name似乎更加可敬。
对于更快的,我不知道。 我假设ExecutablePath比GetName()更快,因为在GetName()中需要Reflection,但是应该测量它。
编辑:
尝试构建此控制台应用程序,运行它,然后尝试使用Windows文件资源管理器重命名可执行文件名,直接双击重命名的可执行文件再次运行。
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.