C# AssemblyFileVersion 在程序中的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/699580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:57:46  来源:igfitidea点击:

C# AssemblyFileVersion usage within a program

c#assemblyinfo

提问by Zack

I'm working on a program, and I'm trying to display the assembly FILEversion

我正在开发一个程序,我正在尝试显示程序集文件版本

    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart);
        }
    }

At the moment, this only returns the first two version numbers in the "AssemblyVersion", not "AssemblyFileVersion." I'd really like to just reference the AssemblyFileVersion rather than store an internal variable called "Version" that I have to update both this and the assembly version...

目前,这仅返回“AssemblyVersion”中的前两个版本号,而不是“AssemblyFileVersion”。我真的很想只引用 AssemblyFileVersion 而不是存储一个名为“Version”的内部变量,我必须更新这个和程序集版本......

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("3.5.0")]

That's my AssemblyFileVersion from AssemblyInfo.cs. I'd like to just reference the "3.5.x" part, not the "1.0.*" :/

那是我来自 AssemblyInfo.cs 的 AssemblyFileVersion。我只想引用“3.5.x”部分,而不是“1.0.*”:/

Thanks, Zack

谢谢,扎克

采纳答案by Diadistis

Use ProductMajorPart/ProductMinorPart instead of FileMajorPart/FileMinorPart :

使用 ProductMajorPart/ProductMinorPart 而不是 FileMajorPart/FileMinorPart :

    public static string Version
    {
        get
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.ProductMajorPart, fvi.ProductMinorPart);
        }
    }

回答by shahkalpesh

I guess you will have to use FileVersionInfo class.

我猜你将不得不使用 FileVersionInfo 类。

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

回答by sipwiz

To get the version of the currently executing assembly you can use:

要获取当前正在执行的程序集的版本,您可以使用:

using System.Reflection;
Version version = Assembly.GetExecutingAssembly().GetName().Version;

The Assembly class can also load files and access all the assemblies loaded in a process.

Assembly 类还可以加载文件并访问进程中加载​​的所有程序集。

回答by Vincent Duvernet

using System.Reflection;
using System.IO;

FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);

Console.WriteLine("AssemblyVersion : {0}", Assembly.GetExecutingAssembly().GetName().Version.ToString());

Console.WriteLine ("AssemblyFileVersion : {0}" , fv.FileVersion.ToString ());

回答by Mark Menchavez

    var fileVersion = GetCustomAttributeValue<AssemblyFileVersionAttribute>(assembly, "Version");

    private static string GetCustomAttributeValue<T>(Assembly assembly, string propertyName)
        where T : Attribute
    {
        if (assembly == null || string.IsNullOrEmpty(propertyName)) return string.Empty;

        object[] attributes = assembly.GetCustomAttributes(typeof(T), false);            
        if (attributes.Length == 0) return string.Empty;

        var attribute = attributes[0] as T;
        if (attribute == null) return string.Empty;

        var propertyInfo = attribute.GetType().GetProperty(propertyName);
        if (propertyInfo == null) return string.Empty;

        var value = propertyInfo.GetValue(attribute, null);
        return value.ToString();
    }

回答by makdu

 protected void Application_Start(object sender, EventArgs e)
 {
     _log.InfoFormat("*************{0} **** Version: {1}************  ", Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version);
  }

Output

输出

INFO Global - *************CustomerFile **** Version: 1.0.17.2510************

INFO Global - *************CustomerFile **** 版本:1.0.17.2510************