如何获得正在执行的程序集版本?
我正在尝试使用以下代码在 C# 3.0 中获取正在执行的程序集版本:
I am trying to get the executing assembly version in C# 3.0 using the following code:
var assemblyFullName = Assembly.GetExecutingAssembly().FullName;
var version = assemblyFullName .Split(',')[1].Split('=')[1];
还有其他合适的方法吗?
Is there another proper way of doing so?
两个选项...无论应用程序类型如何,您都可以随时调用:
Two options... regardless of application type you can always invoke:
Assembly.GetExecutingAssembly().GetName().Version
如果是 Windows Forms 应用程序,如果专门查找产品版本,您始终可以通过应用程序访问.
If a Windows Forms application, you can always access via application if looking specifically for product version.
Application.ProductVersion
使用 GetExecutingAssembly
作为程序集引用并不总是一种选择.因此,我个人认为在我可能需要引用底层程序集或程序集版本的项目中创建静态帮助器类很有用:
Using GetExecutingAssembly
for an assembly reference is not always an option. As such, I personally find it useful to create a static helper class in projects where I may need to reference the underlying assembly or assembly version:
// A sample assembly reference class that would exist in the `Core` project.
public static class CoreAssembly
{
public static readonly Assembly Reference = typeof(CoreAssembly).Assembly;
public static readonly Version Version = Reference.GetName().Version;
}
然后我可以根据需要在我的代码中干净地引用 CoreAssembly.Version
.
Then I can cleanly reference CoreAssembly.Version
in my code as required.