33

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

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

[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.*" :/

Thanks, Zack

Benjol
  • 63,995
  • 54
  • 186
  • 268
Zack
  • 2,477
  • 4
  • 37
  • 50
  • 1
    You're already 99% of the way there, just change the return to fvi.FileVersion – McAden Jan 19 '10 at 19:03
  • see https://stackoverflow.com/a/12528418/492 to get the version info from a specific DLL, rather than the parent executing app. – CAD bloke Oct 23 '17 at 00:21

6 Answers6

34

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);
        }
    }
Diadistis
  • 12,086
  • 1
  • 33
  • 55
  • Err. I just realized it's getting the .dll's version! Not exactly what I am wanting. I want the executable version. (Forgot to mention that it's being called from a .dll) – Zack Mar 31 '09 at 00:50
  • 4
    Assembly asm = Assembly.GetEntryAssembly(); – Diadistis Mar 31 '09 at 00:51
  • 5
    Note that fvi.FileVersion will give you the formatted string directly (all four parts), if that's what you need... – Benjol Sep 28 '09 at 13:41
6
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 ());
edosoft
  • 17,121
  • 25
  • 77
  • 111
3
    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();
    }
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
Mark Menchavez
  • 1,651
  • 12
  • 15
2

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.

sipsorcery
  • 30,273
  • 24
  • 104
  • 155
1

I guess you will have to use FileVersionInfo class.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FullpathToAssembly)

shahkalpesh
  • 33,172
  • 3
  • 63
  • 88
0
 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************

makdu
  • 908
  • 2
  • 13
  • 26