C# AssemblyFileVersion usage within a program

拜拜、爱过 提交于 2019-11-28 06:15:30

Use ProductMajorPart/ProductMinorPart instead of 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);
        }
    }
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 ());
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();
    }

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.

I guess you will have to use FileVersionInfo class.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

 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************

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!